var status = 23;
function yzx() {
var status = 123;
xyz = () => {
setTimeout(() => {
console.log(this.status);
}, 100);
};
xyz();
}
yzx();
For further actions, you may consider blocking this person and/or reporting abuse
var status = 23;
function yzx() {
var status = 123;
xyz = () => {
setTimeout(() => {
console.log(this.status);
}, 100);
};
xyz();
}
yzx();
For further actions, you may consider blocking this person and/or reporting abuse
David Neal -
Ogasawara Kakeru -
chintanonweb -
Arindam Majumder -
Top comments (3)
Because
this
is not scope only object context. when you call a methodx.foo()
this inside foo isx
. And if you don't have object context like in your example this is window object.actually the question is more like if i would have done
then i will get 123 because now i am attaching 123 to this of the function xyz()
but where does a var or let attachs itself to when in a function you can point it at by simply
here we have said console.log(status) and we are able to see 123 in console.log()
Check this:
You're not attaching
this
to function. Function doesn't havethis
in this case.This
is window (global object) object here. Don't use this outside of object context. You're using legacy behavior that stops working if you use "use strict" that is default for ES Module and web workers.