我的代碼如下構造器與陣列和用於循環顯示錯誤
// Our Person constructor
function Person (name, age) {
this.name = name;
this.age = age;
}
// Now we can make an array of people
var family= new Array();
family[0]= new Person("alice", 40);
family[1]= new Person("bob", 42);
family[2]= new Person("michelle", 8);
family[3]= new Person("timmy", 6);
// loop through our new array
for(var i = 0; i <= family.length; i++) {
console.log("My name is " + family[i].name);
}
唐,t無爲什麼,但它示出了下面
My name is alice
My name is bob
My name is michelle
My name is timmy
---
We're running a test below to make sure your code works.
My name is aliceMy name is bobMy name is michelleMy name is timmy
TypeError: family[i] is undefined
顯示錯誤我不能夠糾正這個。爲我解決它。
循環應該在'i
Rajesh
數組基於0。第一個索引= 0,最後一個索引=(長度 - 1) – Andreas
其固定,謝謝@Rajesh –