Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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 如何绑定参数';这';在使用forEach()时,是否将其添加到数组的每个元素?_Javascript_Arrays_Foreach_This_Bind - Fatal编程技术网

Javascript 如何绑定参数';这';在使用forEach()时,是否将其添加到数组的每个元素?

Javascript 如何绑定参数';这';在使用forEach()时,是否将其添加到数组的每个元素?,javascript,arrays,foreach,this,bind,Javascript,Arrays,Foreach,This,Bind,语言:Javascript 操作系统:Ubuntu 14.04 浏览器:在Chrome、Chrome和Firefox中测试 背景 我正在制作一个HTML5画布游戏,里面有各种可收藏的物品。这些项目的超类是可收集的,它有一个render()函数: Collectable.prototype.render = function(){ // renders the collectables } Collectable有不同的子类,所有这些子类都将render()委托给超类。例如: var H

语言:Javascript
操作系统:Ubuntu 14.04
浏览器:在Chrome、Chrome和Firefox中测试

背景

我正在制作一个HTML5画布游戏,里面有各种可收藏的物品。这些项目的超类是
可收集的
,它有一个
render()
函数:

Collectable.prototype.render = function(){
    // renders the collectables
}
Collectable
有不同的子类,所有这些子类都将
render()
委托给超类。例如:

var Heart = function(x, y) {
    Collectable.call(this, x, y);
}
Heart.prototype = Object.create(Collectable.prototype);
我通过首先创建一个包含我所有收藏品的数组来实例化我的收藏品:

var collectables = [];
var numOfEach = 5;
// IIFE so as to not pollute global scope
(function() {
    for (var i = 0; i < numOfEach; i++) {
        // TODO: make this cleaner
        var heart = new Heart(100, 200);
        collectables.push(heart);
        var gemBlue = new GemBlue(100, 200);
        collectables.push(gemBlue);
        var gemOrange = new GemOrange(100, 200);
        collectables.push(gemOrange);
    }
})();
…但是在这种情况下,
绑定到
窗口
,因此
此.render
不是一个函数

问题

如何将
绑定到我的
收藏品
数组的每个元素

我的研究

我已经读过了,但是我恐怕无法确定这将如何应用于
forEach()
和我的匿名函数

然后我找到了解释
forEach
如何使用可选参数来设置
。在阅读之后,我尝试了以下方法,但没有成功:

 collectables.forEach(function() {
     // test what 'this' is bound to
     console.log(this);

     // call render method
     this.render();
 }, this);
…这显然与将此设置为全局对象具有相同的效果

 collectables.forEach(function() {
     // test what 'this' is bound to
     console.log(this);

     // call render method
     this.render();
 }, collectables);
…这让我更接近了,因为
这个
作为一个整体绑定到了collectables数组,但显然
这个。渲染仍然不是一个函数。我看不出在
forEach
中可以使用什么参数将
this
绑定到数组的每个元素

我有一种感觉,把所有这些都打出来后,答案会很简单。我对
这个
的概念比较陌生,所以对我放松点

我还没有找到另一个问题来回答这个问题,或者至少是一个根据我的知识水平我能够理解的问题。例如,我考虑了以下问题:


如果我理解正确,您希望获得与每个可收藏物品关联的对象。在这种情况下,只需为
forEach
循环传递一个参数。在本例中,我将该参数称为
theobject
,但
collectable
甚至仅仅是
c
可能更准确

collectables.forEach(function(theObj) {
    // test what 'this' is bound to
    console.log(theObj);

    // call render method
    theObj.render();
});
更新:您在
forEach
调用中传递的
this
正在将
this
的当前值传递给循环。此时,
窗口
的范围内执行,这就是为什么
窗口
。如果不希望引用该窗口,则不需要传递
,而且由于
窗口
是全局的,因此根本不需要传递
,因此我已将其从建议的答案中删除

更新:MDN是JavaScript文档的重要资源:

forEach()
工具将三个参数传递给回调函数:

Collectable.prototype.render = function(){
    // renders the collectables
}
  • 数组的当前元素
  • 将索引放入数组中
  • 数组本身
  • 因此:

    确实不需要绑定此
    ,但是如果您确实想绑定,您可以:

    collectibles.forEach(function callback(collectible) {
      if (this !== collectible)
        callback.call(collectible, collectible);
      else {
        // body of function
      }
    });
    

    尝试
    collectables.forEach.call(collectables,callback)
    您是否有兴趣专门使用
    this
    关键字?如果将数组项作为
    forEach
    回调的一部分,那么您的示例似乎可以运行。例如,
    collectibles.forEach(函数(collectible){collectible.render();})
    另外,传递给
    forEach
    回调的每个对象都有权访问
    render
    方法吗?在这种情况下,回调中甚至不需要
    this
    。您可以将this值作为回调后的第二个参数传递给
    forEach()
    myArray.forEach((e,i,a)=>this.render(e,this)是的,非常简单。我太专注于
    这个
    ,以至于忽略了
    forEach
    的工作原理。你是对的。我接受了另一个答案,只是因为他/她确实在技术上回答了这个问题(就<代码>这个)。问一个有缺陷的问题是我的错!这是一个合理的问题:)
    collectibles.forEach(function callback(collectible) {
      if (this !== collectible)
        callback.call(collectible, collectible);
      else {
        // body of function
      }
    });