[英]Storing objects in an array in javascript is storing the function
我有一個稱為管道的自定義對象
function pipe()
{
this.x = 800;
this.width = 50;
this.y = 450;
}
當我嘗試將管道存儲在數組中時,它不是存儲對象,而是存儲函數。
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);
}
如果我嘗試訪問管道中管道的屬性,則說未定義。
var pipe = pipes[0];
console.log(pipe.x);
您放置pipes.push(pipe)
而不是pipes.push(p)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.