Javascript 为什么';这';绑定到对象内的窗口';主干后的s闭合';s model.fetch?
我有一个javascript/主干脚本,看起来像这样:Javascript 为什么';这';绑定到对象内的窗口';主干后的s闭合';s model.fetch?,javascript,ajax,backbone.js,closures,this,Javascript,Ajax,Backbone.js,Closures,This,我有一个javascript/主干脚本,看起来像这样: var Model = Backbone.Model.extend({}); var View = Backbone.View.extend({ initialize: function() { _.bindAll(this); //bind this this.doProcessing(); }, doProcessing: function() { $('.someElement').each(funct
var Model = Backbone.Model.extend({});
var View = Backbone.View.extend({
initialize: function() {
_.bindAll(this); //bind this
this.doProcessing();
},
doProcessing: function() {
$('.someElement').each(function() {
Model myModel = new Model;
myModel.fetch({ //issue ajax
success: function(model, response){ //after successful ajax
console.log(this) // <-- outputs Window!!
});
}); //end each
} //end processing
});
new View;
doProcessing: function() {
$('.someElement').each(function() {
Model myModel = new Model;
myModel.fetch({
success: function(model, response){
console.log( "into the fetch", myModel );
}
});
};
}
一个解决办法是尝试这样的东西
myModel.fetch({
success: _.bind(function(model, response){
console.log(this);
},this);
});
默认情况下会发生这种情况,因为.call()或.apply()没有给出任何特定的上下文,它会使用
窗口作为此对象块的上下文,或者此引用的内容在javascript中非常不稳定
我认为上下文的更改是在块$('.someElement')中完成的代码>,与Model.fetch()的工作方式无关
尝试添加更多日志,如:
doProcessing: function() {
console.log( "out of the block", this );
$('.someElement').each(function() {
console.log( "begining of block", this );
Model myModel = new Model;
myModel.fetch({
success: function(model, response){
console.log( "into the fetch", this );
}
});
};
console.log( "end of block", this );
}
在中,每个块这个应该是循环中的实际DOM元素
要解决this
的这一变化,意味着我习惯使用的解决方法,我习惯看到的解决方法是将this
引用到块外的另一个变量(当this
是您期望的时),并将此新引用引用到块中:
doProcessing: function() {
console.log( "out of the block", this );
var _self = this;
$('.someElement').each(function() {
console.log( "begining of block", _self );
Model myModel = new Model;
myModel.fetch({
success: function(model, response){
console.log( "into the fetch", _self );
}
});
};
console.log( "end of block", this );
}
更新
如果您想引用最近创建的模型,只需使用您创建的外部引用,如下所示:
var Model = Backbone.Model.extend({});
var View = Backbone.View.extend({
initialize: function() {
_.bindAll(this); //bind this
this.doProcessing();
},
doProcessing: function() {
$('.someElement').each(function() {
Model myModel = new Model;
myModel.fetch({ //issue ajax
success: function(model, response){ //after successful ajax
console.log(this) // <-- outputs Window!!
});
}); //end each
} //end processing
});
new View;
doProcessing: function() {
$('.someElement').each(function() {
Model myModel = new Model;
myModel.fetch({
success: function(model, response){
console.log( "into the fetch", myModel );
}
});
};
}
或者只使用主干发送到成功回调的引用:
“成功回调以某种方式绑定到窗口对象”
不,JavaScript中的函数没有“绑定”。此
的内容仅在调用函数时指定。让我们看看它叫什么
success(model, resp)
因此直接调用函数,而不是通过方法调用语法(someObj.someMethod(…)
),也不是通过.call()
或.apply()
)。因此,在本例中,此
设置为全局对象,在本例中为窗口。似乎不起作用。输出是div元素,每个
都在其上迭代,但如果将此
更改为myModel
作为最后一个要绑定的参数,则myModel
不起作用。但这不是有点难看吗?你在哪里看到的?对调用
的调用包括此
作为上下文,如果我理解正确,则没有执行调用
。。。您的成功处理程序只是定期被调用:if(success)success(model,resp)代码>是的,我确实用过。但这不是重点\u self
指的是最外层的对象,而不是myModel
@PhD-Answer更新,以显示如何传递对最近创建的模型的引用