在下面的代码中,我试图只打印出数组的第一个值(名称),但它并没有像我预期的那样工作:
function Person (name, age) {
this.name = name;
this.age = age;
}// Our Person constructor
// 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(i = 0; i <= family.Length; i++) {
console.log( family[i].this.name);
}
最佳答案
您错误地使用了“this”关键字。当您访问 family[i] 时,您已经在使用 JavaScript 访问该原型(prototype)的一个实例。只需删除“这个”。
关于javascript - 我只想打印数组中的第一个值(名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45302329/