简体   繁体   English

Javascript遍历原型

[英]Javascript looping through prototype

Please consider the code: 请考虑以下代码:

// define the GameObject constructor function

    var GameObject = function(width, height) {
    this.x = Math.floor((Math.random() * myCanvasWidth) + 1);
    this.y = Math.floor((Math.random() * myCanvasHeight) + 1);
    this.width = width;
    this.height = height;
    return this;
};

// (re)define the GameObject prototype object

GameObject.prototype = {
    x: 0,
    y: 0,
    width: 5,
    width: 5,
    draw: function() {
        myCanvasContext.fillRect(this.x, this.y, this.width, this.height);
    }
};

We can then instantiate the GameObject 100 times. 然后,我们可以实例化GameObject 100次。

var x = 100,
arrayOfGameObjects = [];
do {
    arrayOfGameObjects.push(new GameObject(10, 10));
    } while(x--);

Now we have an array of 100 GameObjects, which all share the same prototype and definition of the draw method, which drastically saves memory within the application. 现在,我们有一个由100个GameObject组成的数组,它们全部共享相同的原型和draw方法的定义,从而大大节省了应用程序内的内存。

When we call the draw method, it will reference the exact same function. 当我们调用draw方法时,它将引用完全相同的函数。

var GameLoop = function() {
    for(gameObject in arrayOfGameObjects) {
        gameObject.draw(); // this is my problem. Is this correct? gameObject is simply and index who draw() method gets executed
    }
};

My problem is with the last line code where method draw() is getting executed. 我的问题是方法draw()正在执行的最后一行代码。 Since gameObject is simply an Index, how can draw() method get executed? 由于gameObject只是一个索引,如何执行draw()方法? That index doesn't hold any object. 该索引不包含任何对象。 It is simply an index right? 仅仅是索引吧?

Here is a link 是一个链接

You should really use the following for your GameLoop : 您应该在GameLoop使用以下GameLoop

var GameLoop = function() {
    for(var i = 0; i < arrayOfGameObjects.length; i++) {
        arrayOfGameObjects[i].draw();
    }
};

Use a plain for loop to iterate through an array. 使用普通的for循环遍历数组。

var GameLoop = function() {
    for (var i = 0; i < arrayOfGameObjects.length; i++) {
        arrayOfGameObjects[i].draw();
    }
};

It's actually bad practice to use a for in loop on an array since it's just a round about way to get the indexes that you want. 在数组上使用for in循环实际上是一种不好的做法,因为这只是一种获取所需索引的方式。

I like jquery $.each 我喜欢jquery $ .each

$.each(arrayOfGameObjects, function(i, gObject) {
    gObject.draw();
});

Otherwise as described by others use for with iteration using array length. 否则如其他人描述使用用于使用数组长度迭代。

var GameLoop = function() {
    for(var i = 0, len = arrayOfGameObjects.length; i<len; i++) {
        gameObject.draw();
    }
};

See this working on http://jsfiddle.net/SdBcx/2/ 参见http://jsfiddle.net/SdBcx/2/

Some notes: 一些注意事项:

  • It is bad practice to use the for(obj in array) on Array objects. 在Array对象上使用for(obj in array)是一种不好的做法。 The reason why is, if you add a custom function to Array.prototype , the custom function will appear in your for loop! 原因是,如果您向Array.prototype添加自定义函数,则该自定义函数将出现在您的for循环中! I've written an example of this here: http://jsfiddle.net/BUp6T/ 我在这里写了一个例子: http : //jsfiddle.net/BUp6T/
  • You can see that I am saving the arrayOfGameObjects.length in the len variable. 您可以看到我将arrayOfGameObjects.length保存在len变量中。 This happens only once, before the loop executes. 在循环执行之前,此操作仅发生一次。 This is significantly faster than the common solution which is: for(var i = 0; i < arrayOfGameObjects.length; i++) which retrieves the array length every single loop iteration. 这比常见的解决方案要快得多:常见的解决方案是: for(var i = 0; i < arrayOfGameObjects.length; i++) ,该解决方案在每个单循环迭代中检索数组长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM