Can anybody explain to me why A
is true and B
is false? I would have expected B to be true as well.
function MyObject() {
};
MyObject.prototype.test = function () {
console.log(A, this instanceof MyObject);
(function () {
console.log(B, this instanceof MyObject);
}());
}
new MyObject().test();
update:
since ecmascript-6 you can use arrow functions which would make it easy to refer to MyObject like this:
function MyObject() {
};
MyObject.prototype.test = function () {
console.log(A, this instanceof MyObject);
(() => {//a change is here, which will have the effect of the next line resulting in true
console.log(B, this instanceof MyObject);
})(); //and here is a change
}
new MyObject().test();