Why does the this
in the prototype refer to the global context while the this
in the declaration refers to the function? Even when trying to explicitly set the context of this
it still refers to the global context.
var Foo = function (a) {
console.log(this); // this will refer to global context
this.bar = () => {
// this refers to function context
return a;
};
};
Foo.prototype = {
biz: () => {
return this.bar(); // this this will refer to global context
},
};
var f = new Foo(7);
f.biz(); // this.bar is not a function
f.biz.call(f); // this.bar is not a function