我有一个for循环运行,遍历我的数组中的每个项目并使用方法执行数组项目。由于某种原因,这返回为未定义,但没有.method它工作正常。这是我的代码:
var people = ["susan", "david", "bob", "verity", "rhys", "charles"]
function characters(name, sex) {
this.name = name;
this.sex = sex;
}
var susan = new characters("Susan", "Female");
var david = new characters("David","Male");
var bob = new characters("Bob","Male");
var verity = new characters("Verity","Female");
var rhys = new characters("Rhys","Male");
var charles = new characters("Charles","Male");
characters.prototype.info = function() {
alert("Hi my name is " + this.name + " and I am a " + this.sex);
}
for(i = 0; i < people.length;i++) {
people[i].info();
}
预期的行为是,每个角色都会发出一条警告:“我的名字是[姓名]而我是[性别]”,但没有任何反应。
答案 0 :(得分:3)
实际上,你需要数组中的字符,而不是字符串。这就是为什么它不起作用的原因。你正在循环遍历字符串数组试图调用它们的info函数,但是string没有实现info函数。创建角色后,您需要将它们添加到阵列中,然后您可以循环调用它们以获取所需的警报。
function characters(name, sex) {
this.name = name;
this.sex = sex;
}
var susan = new characters("Susan", "Female");
var david = new characters("David","Male");
var bob = new characters("Bob","Male");
var verity = new characters("Verity","Female");
var rhys = new characters("Rhys","Male");
var charles = new characters("Charles","Male");
characters.prototype.info = function() {
alert("Hi my name is " + this.name + " and I am a " + this.sex);
}
var people = [susan, david, bob, verity, rhys, charles];
for(i = 0; i < people.length;i++) {
people[i].info();
}
&#13;
答案 1 :(得分:3)
people
是字符串的数组,而不是对象。
移动此行:
var people = ["susan", "david", "bob", "verity", "rhys", "charles"]
所以在之后是你声明并为变量赋值的行。
然后删除引号,这样就有变量而不是字符串文字。
var people = [susan, david, bob, verity, rhys, charles];
答案 2 :(得分:3)
您的问题是您的数组是一个字符串数组而不是characters
个对象的数组,这正是您的循环所期望的。
people[i]
不是您创建的对象,而是包含每个对象的变量名称的字符串。因此,调用info()
方法将失败,因为您在字符串而不是characters
对象上调用它。
删除引号并重新排列代码,以便在变量声明之后出现数组声明,并且它将正常工作:
function characters(name, sex) {
this.name = name;
this.sex = sex;
}
characters.prototype.info = function() {
alert("Hi my name is " + this.name + " and I am a " + this.sex);
}
var susan = new characters("Susan", "Female");
var david = new characters("David", "Male");
var bob = new characters("Bob", "Male");
var verity = new characters("Verity", "Female");
var rhys = new characters("Rhys", "Male");
var charles = new characters("Charles", "Male");
var people = [susan, david, bob, verity, rhys, charles];
for (i = 0; i < people.length; i++) {
people[i].info();
}
&#13;
答案 3 :(得分:2)
people
是一个字符串数组。数组中的字符串“suzan”与您进一步声明的变量suzan
之间没有关系。而是尝试将对象本身放入数组中:
function characters(name, sex) {
this.name = name;
this.sex = sex;
}
var susan = new characters("Susan", "Female");
var david = new characters("David","Male");
var bob = new characters("Bob","Male");
var verity = new characters("Verity","Female");
var rhys = new characters("Rhys","Male");
var charles = new characters("Charles","Male");
var people = [susan, david, bob, verity, rhys, charles];
characters.prototype.info = function() {
alert("Hi my name is " + this.name + " and I am a " + this.sex);
}
for(i = 0; i < people.length;i++) {
people[i].info();
}
如果没有每个人的所有特定变量,您甚至可以直接将字符创建的结果放入数组中:
function characters(name, sex) {
this.name = name;
this.sex = sex;
}
var people = [
new characters("Susan", "Female"),
new characters("David","Male"),
new characters("Bob","Male"),
new characters("Verity","Female"),
new characters("Rhys","Male"),
new characters("Charles","Male")
];
characters.prototype.info = function() {
alert("Hi my name is " + this.name + " and I am a " + this.sex);
}
for(i = 0; i < people.length;i++) {
people[i].info();
}