Javascript对象,我在做什么错?

最后发布: 2015-07-19 23:50:22


问题

我从编码练习中得到以下代码。 当我尝试提交它时,我得到一个

语法错误:意外的字符串

var movieObj = {
"Toy Story 2": "Great story. Mean prospector.",
"Finding Nemo": "Cool animation, and funny turtles."
"The Lion King": "Great songs."
};

var getReview = function (movie) {
    if (movie in movieObj) {
        return movieObj[movie]
    } else {
        return "I don't know!"
    }
};

getReview("Toy Story 2") //expected = "Great story. Mean prospector."
getReview("Toy Story") //expected = " don't know!"

我究竟做错了什么?

javascript object javascript-objects
回答

实际上,您只是缺少了movieObj对象第二个项目的逗号。 将第二行替换为"Finding Nemo": "Cool animation, and funny turtles.", /* Notice the comma */之后应该可以正常工作。


回答

你忘了一个逗号后“......海龟”对象属性与分离,


回答

您在movieObj中缺少"Finding Nemo"的逗号。此外,还缺少了一些分号;

var movieObj = {
    "Toy Story 2": "Great story. Mean prospector.",
    "Finding Nemo": "Cool animation, and funny turtles.",
    "The Lion King": "Great songs."
};

var getReview = function (movie) {
    if (movie in movieObj) {
        alert(movieObj[movie]);
    } else {
        alert("I don't know!");
    }
};

getReview("Toy Story 2"); //expected = "Great story. Mean prospector."
getReview("Toy Story"); //expected = " don't know!"