我很好奇这怎么写得更好:
function Klass(variable) {
this.variable = variable;
this.callAjax = function() {
$.get('/url', { }, function(json) {
console.log(variable); //! <-- shows undefined
}, "json");
}
}
所以我创建了一个局部变量:_variable
function Klass(variable) {
this.variable = variable;
this.callAjax = function() {
var _variable = this.variable;
$.get('/url', { }, function(json) {
console.log(_variable); //! <-- its ok
}, "json");
}
}
它很好,但我真的没有这个解决方案,
你们中有人有更好的代码吗?