I'm curious how could this be written better:
function Klass(variable) {
this.variable = variable;
this.callAjax = function() {
$.get('/url', { }, function(json) {
console.log(variable); //! <-- shows undefined
}, "json");
}
}
so I create a local variable: _variable
function Klass(variable) {
this.variable = variable;
this.callAjax = function() {
var _variable = this.variable;
$.get('/url', { }, function(json) {
console.log(_variable); //! <-- its ok
}, "json");
}
}
and its fine, but I really don't this solutions,
Does someone of you have a better code?
$.get(...function(){...}.bind(this))console.log(variable);should showundefined.variablesshould refer to the constructor argument. Do you passundefinedto the constructor and setthis.variablelater?