Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does 'this' refer to in Javascript functions

Tags:

javascript

I understand the general idea behind the this keyword but I'm having trouble figuring out what it actually refers to in practice. For example, in both these example exercises, I guessed the wrong number.

for question1, I said that the alert would be '5', because it is referring to the this.x outside the anonymous function in the function.

In question2, I thought the alert would be 5 because this line

var alertX = o.alertX;

would bind the value 5 for property x inside the variable o to the new variable 'alertX' which becomes the function call in the next line: alertX();

Can you explain why I'm wrong?

var question1 = function() {
    this.x = 5;
     (function() {
        var x = 3;
        this.x = x;
    })();
    alert(this.x);
};
var answer1 = 3; 


var question2 = function() {
    this.x = 9;
    var o = {
        'x':5,
        'alertX':function() { alert(this.x); }
    };
    var alertX = o.alertX;
    alertX();
}
var answer2 = 9; 
like image 629
BrainLikeADullPencil Avatar asked Sep 04 '12 23:09

BrainLikeADullPencil


People also ask

What does this refer to in a function?

“this” Refers to a Global Object By default, the execution context for an execution is global — which means if a code is being executed as part of a simple function call, then this refers to a global object. The window object is the global object in the case of the browser.

What does '$' mean in JavaScript?

Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

What is the value of this in JS?

The value that this stores is the current execution context of the JavaScript program. Thus, when used inside a function this's value will change depending on how that function is defined, how it is invoked and the default execution context.

What is this in JavaScript class?

When a function is used as a constructor (with the new keyword), its this is bound to the new object being constructed. Note: While the default for a constructor is to return the object referenced by this , it can instead return some other object (if the return value isn't an object, then the this object is returned).


1 Answers

In the first case, when you invoke a method with no explicit receiver this is the Global object (the window in a web browser).

Similarly in the second case: even though the function is defined on the object, and you are inside another, by invoking the function with alertx() the this is set to the Global/window.

In short:

  • For foo.bar(), the this inside of bar will be foo.
  • For bar(), the this will be the Global object.
    • This includes so-called "self-invoking lambdas", i.e. (function(){ ... })().
  • For bar.call(whee) and bar.apply(whee), the this will be whee (because that's what these methods do).

Another example:

var o1 = { name:"o1", f:function(){ console.log(this.name) } };
var o2 = { name:"o2" };
o2.gogogo = o1.f;
o2.gogogo(); // Will output "o2"
like image 83
Phrogz Avatar answered Sep 23 '22 13:09

Phrogz