有人可以向我解释该段落的含义,以及例如在没有引号的情况下它如何不会给出错误?

问题描述 投票:1回答:1

这里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)
javascript object-literal
1个回答
0
投票

对象只能将键作为stringsymbol,否则其他任何键都将在内部转换为字符串。

对象在内部将其键转换为字符串,因此1"1"都相同。因此在这里,upperLeft和lowerLeft不会在这里抛出任何错误,因为它将它们仅视为字符串]

let obj = {
  1: '1',
  hey: 'hey'
}

console.log(obj["1"])
console.log(obj[1])
console.log(obj[1] === obj['1'])
© www.soinside.com 2019 - 2024. All rights reserved.