Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript "this" scope giving different result depending on the way it is called

All,

This is the code:

var Person = function (name) {

  this.name = name;

  this.printName = function(){
    console.log("My name is " + this.name);
  }

};


var p = new Person("Steve");

var funcRef = p["printName"];

p.printName();//Works

p["printName"]();//Works
funcRef();//returns incorrect value

Find a working example here: http://plnkr.co/edit/57LS6oXPfqccAWf6uqQV?p=preview

My question is what's the difference between the last two? I'm accessing the object method in the same way, the only difference is the way it's being called.

Why does it return a difference result?

First time I have come across this in javascript. I understand it's in a difference scope but i don't know how it got decoupled from the object which is what I'd like to understand.

Thanks

Steve

like image 670
Steven Yates Avatar asked Dec 14 '15 16:12

Steven Yates


People also ask

What are the two different scopes in JavaScript?

JavaScript has two scopes: global and local. Local scope has two variations: the old function scope, and the new block scope introduced with ES6. It's worth noting that function scope is actually a special type of a block scope.

What is scope in JavaScript name the different types of scopes?

JavaScript has 3 types of scope: Block scope. Function scope. Global scope.

What is scope pollution in JavaScript?

'Polluting' the global scope means that you define too many variables that are globally accessible. From a programming perspective, you make it difficult to debug and maintain code when you use global variables.

What is difference between lexical scope and closure in JavaScript?

The lexical scope allows a function scope to access statically the variables from the outer scopes. Finally, a closure is a function that captures variables from its lexical scope. In simple words, the closure remembers the variables from the place where it is defined, no matter where it is executed.


2 Answers

javascript bind the this keyword when you call the function on the object directly.

With test.fn(), this will be test inside fn. Same with test['fn'](). But if you do var fn = test.fn; fn(), this will be the global root (window in a browser) inside fn.

You can force the this inside a function like this : var fn = test.fn.bind(test);

More informations here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

like image 89
Magus Avatar answered Oct 08 '22 05:10

Magus


Case1 : 'this' always takes the context of the object with respect to which its called.

In p.printName() the context is p therefore 'this' references the Person object that 'p' refers to.

Case2: But, when you direct 'funcRef' to p's method it loses this context and 'this' references the global object.

The global object can be different depending on your js environment (like the browser context or node and so on).

That is why you see different results.

like image 31
trk Avatar answered Oct 08 '22 07:10

trk