2

//Code starts

var self = this;
var a=10;

function myFunc(){
    this.a = 4;
    var a = 5;
    console.log("this.a -> " + this.a);   //Output: this.a -> 4
    console.log("self.a -> " + self.a);   //Output: self.a -> undefined
    console.log("a -> " + a);    //Output: a -> 5
}
myFunc();

//Code ends

Can someone please explain the Output of the above console.log statements. Thank You.

1
  • this.a and a are different object Commented Feb 17, 2017 at 13:09

3 Answers 3

2

In Javascript, every scope level has a this. When you save this in the var self outside of any function (top level), it gets the window object. So the first this (stored in self) is different from the one in the function. No property "a" was defined on the top level, so self.a is undefined.

Now there is a difference between a variable (var) and a propery (obj.prop). The first one will be resolved from scopes (if a is not defined in the current scope, it will search if it is defined in higher scopes, until it finds window where "global vars" are, and comes undefined if still not found). The second one will search the property through the prototype chain.

Sign up to request clarification or add additional context in comments.

4 Comments

I got your point. The trouble I am still facing is that as you said that since "No property "a" was defined on the top level, so self.a is undefined.". If I create a property a=10 as the first line of the above code, even then I am getting the result of self.a as undefined. Ideally, Isn't it that all 3 ie. [window.a], [this.a] and [a] written in global scope should mean the same? I am getting the answer for self.a only when I am using "this.a=10" in the global scope.
yes, because "a" in "self.a" is a propery, not a var. If you try either "window.a", "window['a']" (anywhere) or "this.a" (in top level), your self.a will be defined in the function.
Is "var a" and "a" same in the global scope?
in the global scope yes (if you except strict mode in which all vars have to be declared with "var", or else it raises an error). The reason is that if you assign a var without the "var" keyword, it will be trated as global, any place where it is assigned (not a good practice)
2
    console.log("this.a -> " + this.a);   //Output: this.a -> 4

The this keyword refers to the object the function belongs to, or the window object if the function belongs to no object. That said you created a varaible a on top and updated that inside the function. Thus 4

    console.log("self.a -> " + self.a);   //Output: self.a -> undefined

No. self referring to window and window have variable a. It should print 4. Check again.

    console.log("a -> " + a);    //Output: a -> 5

That a= 5 shadowed your global variable a declared outside function. You accessed the more specific variable. Local variable have more specific.

2 Comments

I get 2 different outputs at 2 different places: jsfiddle output: self.a-> 4 and running using nodejs outputs: self.a -> undefined
Anyways my doubt is: isn't the "this" in the global scope different from the one used inside the function.
0

I tried the code, self.a also is coming as 4, which is fine.

2 Comments

I don't have enough reputation to comment, that's why
But I got the output as 4 for self.a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.