这里upperLeft和LowerRight不在引号中,但是为什么它没有引发任何错误?
例:-对象文字可以嵌套。例如:var rectangle = { upperLeft: { x: 2, y: 2 }, lowerRight: { x: 4, y: 5 } };
这是关于javascript中的对象初始值设定项。
paragraph:-
The expressions in an object initializer are evaluated each time the object <br/> initializer is evaluated, and they need not have constant values: they can be
arbitrary JavaScript expressions. Also, the property names in object literals may be strings rather than identifiers (this is useful to specify property names that are reserved words or are otherwise not legal identifiers)
对象只能将键作为string或symbol,否则其他任何键都将在内部转换为字符串。
对象在内部将其键转换为字符串,因此1
和"1"
都相同。因此在这里,upperLeft和lowerLeft不会在这里抛出任何错误,因为它将它们仅视为字符串]
let obj = {
1: '1',
hey: 'hey'
}
console.log(obj["1"])
console.log(obj[1])
console.log(obj[1] === obj['1'])