class A
constructor: () ->
setTimeout @b
b: =>
console.log @c
c: '!'
Output:
class A {
constructor() {
setTimeout(this.b);
this.b = this.b.bind(this);
}
b() {
return console.log(this.c);
}
}
A.prototype.c = "!";
This code will produce an error because b() is passed to setTimeout before it is bound to the instance, so it will be invoked with window or undefined as this.
Input:
Output:
This code will produce an error because
b()
is passed tosetTimeout
before it is bound to the instance, so it will be invoked withwindow
orundefined
asthis
.