-1

为什么下面的 javascript 代码对我不起作用?我应该在下面的代码中进行哪些更改?

function person(firstname, lastname, age, eyecolor)
{
    this.firstname = firstname;
    this.lastname = lastname;
    this.age = age;
    this.eyecolor = eyecolor;
}

for(var i = 0; i < 10; i++)
{
    myFather[i] = new person("John", "Doe", i, "blue");
}

for(var i = 0; i < 10; i++)
{
    document.write(myFather[i].firstname + " is " +
                   myFather[i].age + " years old.");
}
4

2 回答 2

3

我看不到myFather在任何地方被初始化。在将数组值分配给它之前,您需要这样做。

var myFather = [];
于 2013-04-21T06:56:59.117 回答
1

myFather 变量未初始化:

var myFather = new Array();
于 2013-04-21T06:58:23.603 回答