Answer the question
In order to leave comments, you need to log in
How to stop using that?
Standard situation
//backbone
render: function () {
var that = this;
App.TemplateManager.get(this.template, function (template) {
var temp = _.template(template);
var html = $(temp(that.model.toJSON()));
that.$el.html(html);
});
return this;
},
//jquery
var that = this;
$.ajax({
url: '/api/' + id,
success: function (template) {
var tmpl = template;
that.templates[id] = tmpl;
callback(tmpl);
}
});
Answer the question
In order to leave comments, you need to log in
$.ajax({
url: '/api/' + id,
success: function (tmpl) {
this.templates[id] = tmpl;
callback(tmpl);
}.bind(this) // <==
});
One can only shorten its use with arrow functions
class NewObj{
constructor(){
this.some_var = "Some var"
this.func()
}
func(){
let sub_func = () => {
console.log(this)
}
sub_func()
}
}
new NewObj();
// В терминале:
// node test.js
// -> NewObj { some_var: 'Some var' }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question