意外的循环行为

时间:2013-12-20 22:28:30

标签: javascript function

所以我尝试运行下面的代码,但它不会遍历数组,'contacts',并运行printPerson函数。如何在bob和Mary上打印信息。

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

var list = function() {
    var contactsLength = contacts.length;
    for (i=0; i > contacts.length; i++) {
        printPerson(contacts[i]);
    }
};
list();

3 个答案:

答案 0 :(得分:3)

for循环中有一个拼写错误:

for (i=0; i > contacts.length; i++)
            ^------------Change to <

答案 1 :(得分:1)

在for循环中更改i > contacts.length i < contacts.length

答案 2 :(得分:1)

寻找cicle  它应为i < contacts.length而不是i > contacts.length