我正在尝试理解.Notation
和[]
符号之间的区别。在下面的问题中,当我使用if (object[key] === true)
时,我得到了正确的答案。但是当我使用if (object.key === true)
时它不起作用。能否有人解释一下为什么会这样不同。
var myObj = {
one: false,
two: false,
three: false,
four: true,
five: false,
six: false
};
var myFunc = function (object) {
for (var key in object) {
if (object[key] === true) {
return "There is a true value in this object";
} else {
}
}
return "Sorry, there are no true values in this object";
};
Object
,因此最好不要使用相同的变量名,即使大小写不同也是如此。 - thefourtheye