Javascript 对象,我做错了什么?
Javascript Objects, What am I doing wrong?
我有以下来自编码练习的代码。当我尝试提交时,我得到
SyntaxError: Unexpected string
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!"
我做错了什么?
您实际上只是缺少 movieObj 对象的第二项的逗号。将第二行替换为
"Finding Nemo": "Cool animation, and funny turtles.",
/* Notice the comma */
之后它应该可以正常工作。
您忘记在“... turtles”之后用逗号分隔对象属性 ,
您在 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!"
我有以下来自编码练习的代码。当我尝试提交时,我得到
SyntaxError: Unexpected string
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!"
我做错了什么?
您实际上只是缺少 movieObj 对象的第二项的逗号。将第二行替换为
"Finding Nemo": "Cool animation, and funny turtles.",
/* Notice the comma */
之后它应该可以正常工作。
您忘记在“... turtles”之后用逗号分隔对象属性 ,
您在 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!"