var contacts = [ { "firstName": "Akira", "lastName": "Laine", "number": "0543236543", "likes": ["Pizza", "Coding", "Brownie Points"] }, { "firstName": "Harry", "lastName": "Potter", "number": "0994372684", "likes": ["Hogwarts", "Magic", "Hagrid"] }, { "firstName": "Sherlock", "lastName": "Holmes", "number": "0487345643", "likes": ["Intriguing Cases", "Violin"] }, { "firstName": "Kristian", "lastName": "Vos", "number": "unknown", "likes": ["Javascript", "Gaming", "Foxes"] } ]; function lookUp(firstName, prop){ for(var i = 0;i<contacts.length;i++){ if(contacts[i].firstName !== firstName){ return "No such contact"; }else{ if(contacts[i].hasOwnProperty(prop)){ return contacts[i][prop]; }else{ return "No such property"; } } } }
问题:lookUp("Kristian", "lastName");总是会回到 return "No such contact";想不通是什么原因
因为循环第一次检测到"Akira" !== "Kristian" 函数就return了……