下面是代碼:構造函數或數組或for循環出錯?
// The Person constructor
function Person(name, age) {
this.name = name;
this.age = age;
}
// Now we can make an array of peoples
var family = new Array();
family[0] = Person("alice", 40);
family[1] = Person("bob", 42);
family[2] = Person("michelle", 8);
family[3] = Person("timmy", 6);
// loop through our new array
for (i=0; i < family.length; i++;) {
console.log(family[i].name)
};
這個腳本的預期成果是:
alice
bob
michelle
timmy
但輸出是:
Uncaught TypeError: Cannot read property 'name' of undefined (anonymous function)
這是正確的,但你應該補充你改變了什麼,以及爲什麼一些評論。 – JJJ