I have a code snippet of small javascript program
var obj = {
id: "awesome",
cool: function coolFn() {
console.log( this.id );
}
};
var id = "not awesome";
obj.cool(); // awesome
setTimeout( obj.cool, 100 ); // not awesome
I didn't understand the result of setTimeout
where this.id
refers to global id
. Why is this happening? Why this
binding is lost inside setTimeout
?