I know that this
variable refers to either the global object (window in case of browsers) or the object that contains it (in case is used in an object's method), unless it is differently specified (e.g using the bind method).
Now, I have the following example:
//define an object
var obj = {
param: 'Luciano',
bar: function () {
console.log('"this" inside method: ' + this);
return this;
}
};
var a = obj.bar();
console.log(a);
//
var b = obj.bar;
console.log(b());
the output is:
"this" inside method: [object Object]
Object {param: "Luciano"}
"this" inside method: [object Window]
Window { ... }
I can understand why in case of var a = obj.bar()
the this variable points to the obj
object. Could someone explain to me please (in terms of execution contexts and scope chain) why in case of var b = obj.bar
the this
variable equals to window
object ? I would expect to be equal to obj
too.