3 ms·
I don't know, I kinda like ".bind(this)" to bind the current scope to a callback. class Vector{ constructor(x = 0, y = 0, z = 0){ this.x = x; this
by mschuetz 10y ago
I don't know, I kinda like ".bind(this)" to bind the current scope to a callback.
class Vector{
constructor(x = 0, y = 0, z = 0){
this.x = x;
this.y = y;
this.z = z;
setTimeout(function(){
console.log(this.x + ", " + this.y + ", " + this.z);
}.bind(this), 10);
}
};
var v = new Vector(1, 2, 3); // prints 1, 2, 3
Edit:
Ok I can see this be potentially useful for function chaining, I just don't do that very much.
- erydo 10y agoYour own example is more concisely covered by arrows, which are already available: class Vector { constructor(x=0, y=0, z=0) { this.x = x; this.y = y; this.z = z; setTimeout(() => console.log(this.x + ", " + this.y + ", " + this.z), 10); } }