9

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?

1
  • 2
    As all of the answers have pointed out, you wrote Array instead of array. You can also use player = []; Also, and this is important, your user function is an object definition, so it should be called User and not user (this is not the problem, of course). Commented Apr 28, 2010 at 12:28

4 Answers 4

22

You have a typo in your code.

Change

player = new array();

to

player = new Array();
Sign up to request clarification or add additional context in comments.

1 Comment

Better still: use an array literal (faster): var player = [];
11

I would do

player = [];

instead of

player = new array();

As a sanity check, try doing:

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

3 Comments

player[] = new user("TehMeanie", 1, 1, "naked"); Why wouldn't that work?
Its not like that. player = []; player.push(new user("TehMeanie", 1, 1, "naked"));
player[0] = new user("TehMeanie", 1, 1, "naked"); would work though
7

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

1 Comment

1

I tried this and worked:

player = [{}];

instead of:

player = new Array();

1 Comment

player = [{}] is not initializing an empty array. It's initializing an array with 1 item, which is an empty object. They are not the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.