So i was just testing how the value of this
is affected inside an ES6 class method.
Can someone explain me why the value of this
inside the inner function undefined
in below code?
class testingThis {
outerFunc() {
console.log("the value of this in outerFunc: ", this);
function innerFunc() {
console.log("value of this in innerFunc: ", this);
}
innerFunc();
}
};
var newTest = new testingThis();
newTest.outerFunc();
Why is the value this
not preserved in ES6 (not necessarily ES6 i guess) methods but is preserved in usual functions (like below):
function a() {
console.log("this outer: ", this)
function b() {
console.log("this inner: ", this)
}
b();
};
a();
Both inner and outer this
in above code have the same value (window
).