Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么';这';绑定到对象内的窗口';主干后的s闭合';s model.fetch?_Javascript_Ajax_Backbone.js_Closures_This - Fatal编程技术网

Javascript 为什么';这';绑定到对象内的窗口';主干后的s闭合';s model.fetch?

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

我有一个javascript/主干脚本,看起来像这样:

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更新,以显示如何传递对最近创建的模型的引用