var a = function() {
alert( this ); // [object Window]
alert( a ); // undefined
}.call( a );
Why does this still refer to the window object and a undefined. Alternatively, when I do it the following way, this is equal to the function a is not undefined.
var a = function() {
return this === a; // true
};
a.call(a);
Can anyone explain why these two functions that are seemingly equivalent give different results?
Because in your first example
aisn’t assigned until after the call to your function completes.In detail, here’s what happens in the first example:
aas thethisparameter.ais still undefined,thisfalls back towindowinstead.undefined, which is assigned toa.