2 ms·
That MDN page gives a good example of how it works, but it's not intended to convince doubters with persuasive arguments. Not sure why you don't know what "thi
by lobster_johnson 11y ago
That MDN page gives a good example of how it works, but it's not intended to convince doubters with persuasive arguments.
Not sure why you don't know what "this" is in the second example. The whole point is that it's predictable. "this" is always the instance your method is defined in, unless you bind it to something else, which requires being explicit. I intentionally didn't include any context, but think about it this way:
class Store {
save(object) {
this.client.put("/objects", JSON.stringify(object))
.then(result => this.update(result))
.catch(err => Application.showError(err, "Could not save.")) }
}
}
"this" is extremely useful. I'm not sure how you could argue otherwise. Here's another pattern I use all the time:
this._socket = new WebSocket(`ws://${url}`);
this._socket.onopen = () => {
this._state = 'connected';
this.emit('connected');
};
The lambda syntax allows placing logic lexically where it belongs.