To answer your question directly, ES6 doesn't offer any additional functionality that we can use to avoid binding onError
at its point of invocation. ES6 hasn't done away with the behaviour of JavaScript's execution context.
As a side note, the way that you are declaring your instance methods is illegal and will throw an error. They should be declared as follows:
Person.prototype.init = function () {
request('http://google.fr').on('error', this.onError.bind(this));
};
Person.prototype.onError = function (error) {
console.log(error);
};
Currently your onError
method will not suffer from any errors if passed unbound. This is because you don't use this
inside the onError
method's body:
// Safe unbound method
Person.prototype.onError = function (error) {
console.log(error);
};
// Unsafe unbound method
Person.prototype.onError = function (error) {
console.log(this, error);
// ^^^^
};