I’ve seen How does "this" keyword work within a function?, but I don’t see that it answers the following.
Given this code:
var MyDate = function(date) {
this.date = date;
};
var obj1 = {
foo: new Date(),
bar: new MyDate(this.foo) // this.foo is undefined
};
var obj2 = {};
obj2.foo = new Date();
obj2.bar = new MyDate(this.foo); // this.foo is undefined
var obj3 = {
foo: new Date(),
bar: new MyDate(obj3.foo)
};
var obj4 = {};
obj4.foo = new Date();
obj4.bar = new MyDate(obj4.foo);
Why do the first two attempts fail, but the last two work? If this isn’t bound to the current object literal, what is it bound to?
Javascript is a late binding language. In fact, it is very late binding. Not only is
thisnot bound during compile time, it is not even bound during runtime (as most other late binding languages do). In javascript,thisis bound during call time.The binding rules are quite different from most other OO languages which is why it seems to confuse a lot of people not familiar with javascript.
Basically, how and where you use
thisin the code does not affect howthisbehaves (it does not matter if it’s a standalone function, an object literal etc.) what determines the value ofthisis how you call the function.The rules are:
1 – When a function is called as a constructor, a new object is created and
thisis bound to that object. For example:2 – When called as an object method
thisrefers to the object the method belongs to. Basically the name before the last dot. For example:3 – If used outside of any function or if a function is not called as a method
thisrefers to the global object. The javascript spec doesn’t give a name to the global object apart from saying that one exists but for browsers it is traditionally calledwindow. For example:4 – In an event handler (such as onclick etc.)
thisrefers to the DOM element that triggered the event. Or for events not associated with the DOM likesetTimeoutorXMLHTTPRequest,thisrefers to the global object. For example:5 – Finally, when a function is called using either the
call()orapply()methodsthiscan be reassigned to anything whatsoever (google “mdn function.prototype.call”). In this way, any object in javascript can borrow/steal another objects’ methods. For example:With
Function.bind()in modern javascript implementations we now have another rule:6 – Functions can also explicitly bind
thisto an object using thebind()method. Thebindmethod returns a new instance of the function wherethisis bound to the argument passed tobind. For example:ECMAscript 5 introduced strict mode which changes the meaning of this in functions that isn’t called as a method or called with call or apply so we have to add a new rule:
7 – When in strict mode,
thisisn’t allowed to refer to the global object (window in browsers). So when a function is not called as a method orthisisn’t bound to anything manually viacallorapplyorbindthenthisbecomesundefined:ECMAscript 6 introduced arrow functions. Arrow functions change how this behaves by binding early.
8 – In arrow functions,
thisis bound at the time the function is declared. Sothisin the following code:behaves as if the function is declared like the following code:
Note that since the
thisin arrow functions are bound at the time the function is declared you can’t use arrow functions if you want to use inheritance. That’s because thethisin the function will always point to the parent object and will never point to the child object. That means that the only way to make inheritance work with arrow function is to override all arrow functions from the parent object.