简体   繁体   English

在javascript中将对象存储在数组中将存储函数

[英]Storing objects in an array in javascript is storing the function

I have a custom object called pipe 我有一个称为管道的自定义对象

function pipe()
{
    this.x = 800;
    this.width = 50;
    this.y = 450;
}

When I try storing pipes in an array its not storing the object, but the function. 当我尝试将管道存储在数组中时,它不是存储对象,而是存储函数。

function loop()
{
    var flappy = new player();
    for(var i = 0; i < 3; ++i)
    {
        var p = new pipe();
        p.x = (1 + i) * 266;
        pipes.push(pipe);
    }
    interval = window.setInterval(function() { updateLogic(flappy) } , 20);
}

If I try accessing a property of a pipe in pipes it says undefined. 如果我尝试访问管道中管道的属性,则说未定义。

var pipe = pipes[0];
console.log(pipe.x);

您放置pipes.push(pipe)而不是pipes.push(p)

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

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