0

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.

zeal
  • 435
  • 1
  • 8
  • 19
  • 1
    A couple of articles for you to read: [Introduction to Object-Oriented JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript) and [Inheritance and the Prototype Chain](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain). – Greg Burghardt Jun 27 '14 at 12:25
  • Perhaps you mean `alert(Friend("cl", "js").favLan === "js");` since you cannot compare the object to another object. You can create a comparison function in the object to return the equalities of the strings inside it – mplungjan Jun 27 '14 at 12:27

0 Answers0