this & "binding"
Who am I?
class Customer {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
sayHi() {
console.log('Hi ' + this.firstName);
}
sayHiLater() {
setTimeout(function () {
this.sayHi();
}, 1000);
}
}
const customer = new Customer('Foo', 'BAR');
customer.sayHiLater(); // ???Binding
The hacky way
The other hacky way
The clean way
Last updated