var movies = [
{
title: "Silence of the Lambs",
rating: "5 stars",
hasWatched: true
},
{
title: "Frozen",
rating: "4 stars",
hasWatched: false
},
{
title: "Inception",
rating: "1 stars",
hasWatched: true
}
]
for (i=0; i<movies.length; i++){
var result = "You have "
if (movies[i].hasWatched=true) {
result += "seen ";
} else {
result += "not seen ";
}
console.log(result + movies[i].title + " - " + movies[i].rating)
}
我试图打印“你已经看过这部电影等”如果hasWatched = true并且“你还没有看过这部电影等”如果hasWatched = false。问题是,在这个版本中,它总是打印“看到”,即使是“冻结”。我试图在if语句中加入“movies [i] .hasWatched = false”,在这种情况下,它总是打印出未看到的。可能是什么问题呢?不能做出正确的如果陈述与对象相关
编辑:感谢球员这是一个非常愚蠢的错误。 Ty大家。
'='应该是''==或更好的''===重复 – Andreas
https://stackoverflow.com/questions/13851399/javascript-if-statements-not-working。 – 2017-05-30 06:37:37
任何棉绒都会挑选它。 – 2017-05-30 06:40:24