In ch2 this part: Lexical this
It said at last:
Embrace this-style mechanisms completely, including using bind(..) where necessary, and try to avoid self = this and arrow-function "lexical this" tricks.
Could it possible to avoid using self=this and arrow-function in the example which is illustrated?
function foo() {
setTimeout(() => {
// `this` here is lexically adopted from `foo()`
console.log( this.a );
},100);
}
var obj = {
a: 2
};
foo.call( obj ); // 2
Could it possible to avoid using self=this and arrow-function
Yes:
function foo() {
setTimeout(function() {
console.log( this.a );
}.bind(this),100);
}
var obj = {
a: 2
};
foo.call( obj ); // 2
@getify beautiful:heart:
Most helpful comment
Yes: