Javascript对象,我做错了什么?

时间:2015-07-19 23:50:23

标签: javascript object javascript-objects

我在编码练习中有以下代码。当我尝试提交时,我得到了

  

SyntaxError:意外的字符串

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!"

我做错了什么?

3 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

在“... turtles”对象属性与,

分隔后,您忘记了逗号

答案 2 :(得分:0)

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!"

function forEach(array, action) {
    for (var i = 0; i< array.length; i++)
        action(array[i]); // array[i] is the argument
}