开发者

Javascript pushing object into array

开发者 https://www.devze.com 2022-12-28 05:51 出处:网络
Hey, I currently am having trouble trying to get this to work. Here\'s a sample code of what I am trying. A lot has been taken out, but this should still contain the problem. I have an object, user, a

Hey, I currently am having trouble trying to get this to work. Here's a sample code of what I am trying. A lot has been taken out, but this should still contain the problem. I have an object, user, and an array, player. I am trying to m开发者_JS百科ake an array with the players in it, here:

function user(name, level, job, apparel)
{
 this.name = name;
 this.state = "alive";
 this.level = level;
 this.job = job;
 this.apparel = apparel;
}

player = new array();
player.push(new user("Main Player", 1, 1, "naked"));
document.write(player[0].name);

But it's not working, nothing's being echo'd. What am I doing wrong?


You have a typo in your code.

Change

player = new array();

to

player = new Array();


I would do

player = [];

instead of

player = new array();

As a sanity check, try doing:

document.write("Name: " + player[0].name);


Well, you've got an error. It's not array but Array.


I tried this and worked:

player = [{}];

instead of:

player = new Array();
0

精彩评论

暂无评论...
验证码 换一张
取 消