Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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';这';关键词_Javascript_Underscore.js_This - Fatal编程技术网

JavaScript';这';关键词

JavaScript';这';关键词,javascript,underscore.js,this,Javascript,Underscore.js,This,在上面的代码中,这个内部的.each()函数指向全局窗口对象,因为.each()由窗口调用。但是,我仍然希望此指向播放视图。我遇到过许多类似的情况,我经常定义一个变量来存储这个变量的初始值,就像所提供的示例中的realThis变量一样。有没有其他常规方法来处理这个问题 注意:我正在按照这本书学习主干,它以下面的代码为例 var PlaylistView = Backbone.View.extend({ el: '#expanded-container', initialize: fun

在上面的代码中,
这个
内部的
.each()
函数指向全局
窗口
对象,因为
.each()
由窗口调用。但是,我仍然希望
指向
播放视图
。我遇到过许多类似的情况,我经常定义一个变量来存储这个变量的初始值,就像所提供的示例中的
realThis
变量一样。有没有其他常规方法来处理这个问题

注意:我正在按照这本书学习主干,它以下面的代码为例

var PlaylistView = Backbone.View.extend({
  el: '#expanded-container',

  initialize: function() {
    this.bg = chrome.extension.getBackgroundPage();
    this.$('.list-group').empty();
    var realThis = this;
    _.each(this.bg.Playlist.models, function (song) {
      // append to playlist, rendering song template?
      var songView = new SongView({ model: song });
      console.log(realThis); // THIS is what I want
      console.log(this) // this is NOT what I want
      //this.$el.append(songView.render().el); // hence, this does NOT work
      realThis.$el.append(songView.render().el); // and THIS works
    });
  }
});
var ListView=Backbone.View.extend({
render:function(){
//假设我们的模型公开了我们将要公开的项目
//显示在我们的列表中
var items=this.model.get('items');
//使用下划线循环浏览我们的每个项目
//每个迭代器
_.每个(项目、功能(项目){
//创建ItemView的新实例,传递
//它是一个特定的模型项
var itemView=newitemview({model:item});
//itemView的DOM元素附加在它之后
//已呈现。此处的“returnthis”很有帮助
//当itemView呈现其模型时。稍后,我们要求
//其产出(“el”)

this.$el.append(itemView.render().el);//您可以使用
.bind()
更改特定函数的
this

这会提醒“酒吧”

将外部
保存为
realThis
that
如果您想同时访问内部和外部
,这也很常见


下划线使用
.call()
来更改传递给它的迭代器函数的
。具体地说,
\uuu0.each()
有第三个参数,允许您指定您想要的
是什么,因此该示例是正确的。请参阅。

您可以使用
.bind()
更改特定函数的

这会提醒“酒吧”

将外部
保存为
realThis
that
如果您想同时访问内部和外部
,这也很常见


下划线使用
.call()
来更改传递给它的迭代器函数的
。具体地说,
\uuu0.each()
有第三个参数,允许您指定您想要的
是什么,因此该示例是正确的。请参阅。

您可以使用
.bind()
更改特定函数的

这会提醒“酒吧”

将外部
保存为
realThis
that
如果您想同时访问内部和外部
,这也很常见


下划线使用
.call()
来更改传递给它的迭代器函数的
。具体地说,
\uuu0.each()
有第三个参数,允许您指定您想要的
是什么,因此该示例是正确的。请参阅。

您可以使用
.bind()
更改特定函数的

这会提醒“酒吧”

将外部
保存为
realThis
that
如果您想同时访问内部和外部
,这也很常见

下划线使用
.call()
更改传递给它的迭代器函数的
。具体来说,
每个()
都有第三个参数,允许您指定希望此
的内容,以便示例正确。请参阅。

From:

每个<代码>\每个(列表、迭代器、[上下文])

迭代器绑定到上下文对象(如果已传递)

initialize()
中,
指向您的主干视图。如果将
作为第三个参数传递给
每个()
将引用迭代器函数中的主干视图

我遇到过很多类似的情况,我经常定义一个变量 它存储了这个的初始值,就像这个变量 所提供的例子。有没有其他常规方法来处理 这个

是。如果您在ES5(非IE8)环境中,请使用。为了向后兼容,请使用

发件人:

每个<代码>\每个(列表、迭代器、[上下文])

迭代器绑定到上下文对象(如果已传递)

initialize()
中,
指向您的主干视图。如果将
作为第三个参数传递给
每个()
将引用迭代器函数中的主干视图

我遇到过很多类似的情况,我经常定义一个变量 它存储了这个的初始值,就像这个变量 所提供的例子。有没有其他常规方法来处理 这个

是。如果您在ES5(非IE8)环境中,请使用。为了向后兼容,请使用

发件人:

每个<代码>\每个(列表、迭代器、[上下文])

迭代器绑定到上下文对象(如果已传递)

initialize()
中,
指向您的主干视图。如果将
作为第三个参数传递给
每个()
将引用迭代器函数中的主干视图

我遇到过很多类似的情况,我经常定义一个变量 它存储了这个的初始值,就像这个变量 所提供的例子。有没有其他常规方法来处理 这个

是。如果您在ES5(非IE8)环境中,请使用。为了向后兼容,请使用

发件人:

每个<代码>\每个(列表、迭代器、[上下文])

迭代器已绑定
var ListView = Backbone.View.extend({
  render: function(){

    // Assume our model exposes the items we will
    // display in our list
    var items = this.model.get('items');

    // Loop through each of our items using the Underscore
    // _.each iterator
    _.each(items, function(item){

      // Create a new instance of the ItemView, passing 
      // it a specific model item
      var itemView = new ItemView({ model: item });
      // The itemView's DOM element is appended after it
      // has been rendered. Here, the 'return this' is helpful
      // as the itemView renders its model. Later, we ask for 
      // its output ("el")
      this.$el.append( itemView.render().el ); // <--- *THIS IS WRONG?
    }, this);
  }
});
function foo() {
    alert(this.x);
}
var boundFoo = foo.bind({x: "bar"});
boundFoo();
var func = function (greeting) {
  return greeting + ': ' + this.name;
};

if (usingUnderscore) {
  func = _.bind(func, {name: 'moe'}, 'hi');
} else if (es5environmentHooray) {
  func = func.bind({name: 'moe'}, 'hi');
}

func();
=> 'hi: moe'