You can pass your method directly as callback and with .bind
, so it runs in the scope you need:
class Cliente {
showName(data) {
alert(data.name);
}
getName() {
$.get('/url').done(this.showName.bind(this));
}
}
If you are using a Babel compiler that accepts TC39 proposals you can use Public Class Fields , like this:
class Cliente {
showName = ({name}) => alert(name);
getName() {
$.get('/url').done(this.showName);
}
}
Example of the second option to work:
class Cliente {
showName = ({name}) => alert(name);
getName() {
$.get('/url').done(this.showName);
}
}
// estas linhas são só para simular o que '$' faz:
let $ = function() {};
$.prototype.get = function() {return this;};
$.prototype.done = function(fn) {
fn.call(window, {name: 'teste!'});
};
$ = new $();
new Cliente().getName();