-3

code 1:

 var name = "The Window";
  var object = {
    name : "My Object",
    getNameFunc : function(){
      return function(){
        return this.name;
      };
    }
  };
  alert(object.getNameFunc()());

It alert "The Window"

code 2:

var name = "The Window";
  var object = {
    name : "My Object",
    getNameFunc : function(){
      var that = this;
      return function(){
        return that.name;
      };
    }
  };
  alert(object.getNameFunc()());

It alert "My Object".

I know the code 2 rename "this" of "that".But I don't know why code 1 alert "The Window".

alan_stack
  • 52
  • 5
  • Is my answer helpful?, or you need more information??? – Parag Bhayani Jun 12 '16 at 08:16
  • Thanks a lot,it do helpful. – alan_stack Jun 12 '16 at 08:24
  • then you can accept and upvote it... – Parag Bhayani Jun 12 '16 at 08:48
  • You know, there is this really fantastic tool called DOCUMENTATION. Oh, and the other one: GOOGLE.COM. There are even lots of blogs, websites, youtube videos. SO is kind of the last place where you should ask question when everythinig has failed you. Just saying. Plus many of these kind of questions have already been asked and answered many times. – Azamantes Jun 12 '16 at 09:42

3 Answers3

1

Lots off answers clarifying how javascript scope works! But I do have a question about your code:

Why two return statements in your function?

statements in your function, i.e.

return function(){
    return this.name;
}

is wrong, cuase it first get returned fron your function to current scope, i.e. window/document. Thus the inner statement is actually called for current scope, thus returning "The Window"!

Insted you have to simply call

return this.name;

Without a wrapper function, now it will return "the Object" cause it is returning in object scope!

demonofthemist
  • 4,081
  • 4
  • 26
  • 45
0

Tips for "this": this points to the Object(not the function) of the current context.

so here context is Important.

so In code1 - this is inside the anonymous function so it is in global context, so here, it is pointing to window object. i.e. here it will print: window.name = The Window

In code2 - this is inside the instance function of created object so it is in object context, so here, it is pointing to object.

i.e. here it will print : object.name = My Object

hope it will help :) thanks :)

Brajendra Swain
  • 355
  • 2
  • 8
  • Well, so `function(){return this.name}` here is in global context,`this` point to window,and `var name` is equivalent to `window.name`? – alan_stack Jun 12 '16 at 08:20
  • yes, as the declaration is not inside any function, that means it is declared in global scope. And all the global variables are belong to window object, so here `window.name` is pointing to `var name`. – Brajendra Swain Jun 12 '16 at 12:37
-1

this may be the problem about "closure",you may would like to see this: How does closure work

In code 1,this is under the device,so it direct to device's name.

And var name here is global var.So you will get The Window

Community
  • 1
  • 1