i am new to js.
and I don’t really understand how this
works.
I have a few examples and they work differently in codesandbox.io, use strict and not use strict.
example 1
var obj = {
id: "awesome",
cool: function coolFn() {
console.log( this);
}
};
var id = "not awesome";
obj.cool();
setTimeout( obj.cool, 100 );
in codesandbox Object {id: "awesome", cool: function coolFn()}, null
.
in use strict {id: "awesome", cool: ƒ},Window{...}
.
in not use strict {id: "awesome", cool: ƒ},Window{...}
.
example 2
var obj = {
id: "awesome",
cool: () => {
console.log(this);
}
};
var id = "not awesome";
obj.cool();
setTimeout(obj.cool, 100);
in codesandbox undefined, undefined
.
in use strict Window{...},Window{...}
.
in not use strict Window{...},Window{...}
.
who can explain what is happening in my examples ?