本文介绍了JavaScript的对象推到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前遇到了麻烦试图让这个工作。这里是什么我想一个样本code。很多已经被取出,但是这仍然应该包含的问题。我有一个对象,用户和数组,播放器。我试图与它的球员,在这里一个数组:

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 make 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?

推荐答案

我会做

player = [];

而不是

player = new array();

作为一个全面的检查,尝试做:

As a sanity check, try doing:

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

这篇关于JavaScript的对象推到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:59