我的JavaScript代码有什么问题?

What is wrong with my JavaScript code

本文关键字:问题 什么 JavaScript 代码 我的      更新时间:2023-09-26

下面是我正在做的练习:http://www.codecademy.com/courses/building-an-address-book/0/3?curriculum_id=506324b3a7dffd00020bf661#

Here is my code:
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];
// printPerson added here
function printPerson(person){
    console.log(firstName);
    console.log(lastName);
};
function printPerson(){
        console.log(contacts[0]);
}
function printPerson(){
        console.log(contacts[1]);
}
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];
// printPerson added here
function printPerson(person){
    console.log(person.firstName + " "+ person.lastName);   
}
printPerson(contacts[0]);
printPerson(contacts[1]);

您希望对代码的最后两部分执行printPerson,而不是重新创建函数

function printPerson(person){
    console.log(person.firstName);
    console.log(person.lastName);
};
printPerson(contacts[0]);
printPerson(contacts[1]);

你写的

function printPerson(){
        console.log(contacts[0]);
    }
function printPerson(){
        console.log(contacts[1]);
    }

你应该写

printPerson(contacts[0]);
printPerson(contacts[1]);