Seems to me you are both right.
Using ‘this’ inside a function that is used as a parameter for a function will make said ‘this’ point to the window object.
As a workaround, the ‘this’ can be assigned to a variable (‘that’) which is then used as a parameter. So, if “var that = this;” was added to the square() function it should work like so:
function square(side){
this.side = side;
var that = this;
this.area = calcArea(that);
}
function calcArea(obj){
return obj.side * obj.side;
}
So the way I see it, either answers A, B, C and D are all wrong or the question is incomplete.
I think the correct answer is B, coz in option D keyword ‘this’ will point to global object (window)
It can’t be the option B, because the calcArea function is calling (this.area = calcArea;) without arguments. The correct answer is D.
Seems to me you are both right.
Using ‘this’ inside a function that is used as a parameter for a function will make said ‘this’ point to the window object.
As a workaround, the ‘this’ can be assigned to a variable (‘that’) which is then used as a parameter. So, if “var that = this;” was added to the square() function it should work like so:
function square(side){
this.side = side;
var that = this;
this.area = calcArea(that);
}
function calcArea(obj){
return obj.side * obj.side;
}
So the way I see it, either answers A, B, C and D are all wrong or the question is incomplete.
answer D is correct
function square(side) {
this.side = side;
this.area = calcArea;
}
function calcArea() {
return this.side * this.side;
}
var square = new square(10);
console.log(square.area()); //result 100
True, answer D is correct