Here is javascript function :
case(1)
--(1)
function Friend(area, favLan){
this.area = area;
this.favLan = favLan;
this.context = this;
return(this);
}
alert(new Friend("cl", "js") === "<what????>");
//what should be written at the place of "" so that alert gives output "true".
Like in the case given below:- case(2)
--(2)
function Friend(area, favLan){
this.area = area;
this.favLan = favLan;
this.context = this;
return(this);
}
alert(Friend("cl", "js") === "<what????>");
The value to be written at the place of "" should be "window"(removing quotation marks) so that the output is true
.
I understand that in second
case the this will owned by the window object as it in the global context but in the first case
this will be owned by the object created with new
keyword but what is the value of the this
here??
It would be really appreciated if one can explain, why the output of following javascript is "The Window"
???
var name = "The Window";
var object = {
name : "My Object",
getNameFunc : function(){
return function(){
return this.name;
};
}
};
alert(object.getNameFunc()()); // outputs <The Window>
Thanks for the help.