是否允许将 undefined 作为 Object 属性 名称?
Is undefined allowed as Object property name?
javascript 允许这样做吗?
a = {undefined : 1}
console.log(a[undefined])
[编辑] 我想我有点搞砸了 python 的习惯,但我实际上考虑过这个:
a = {[undefined] : 1}
console.log(a[undefined])
请注意,它起作用的原因是相同的:undefined
被转换为字符串(就像您使用此 synthax 传递的任何值一样)并且您最终定义了 "undefined"
prop
[/编辑]
如果是这样,是否所有浏览器都以相同的方式处理它,是否在标准中明确规定了?
[编辑]
注意:
这个问题与 'undefined' variable works as key to object with 'undefined' property name 略有不同,尽管根本原因和答案是相同的。
我在 undefined
似乎与 "is it the value undefined
or the string "undefined"
? " 不明确的上下文中询问,而链接的问题清楚地说明 "undefined"
作为道具名称并询问名称与关键字的冲突。
...而且我认为它可以帮助其他程序员使用 Python 或使用 json-like object/dict/hash synthax
的其他语言
[/编辑]
Is this allowed in javascript ?
是的。
And if so, does all browsers handle it the same
是的。
is it explicitly specified in the standards ?
没有。
这只是 undefined
being converted to "undefined"
when used in string context 的结果:
console.log( ("" + undefined).toUpperCase() )
允许作为对象属性名称
访问
a = {undefined : 1}
console.log(a.undefined)
对象 属性 名称始终是字符串。因此,无论您作为 属性 名称传递什么,都会转换为字符串。如果您传递 undefined,那么它将变为 "undefined"。所以这个:
let obj = { undefined: 0 }
obj[undefined]
与此相同
let obj = { undefined: 0 }
obj["undefined"]
undefined 在这种情况下没有特殊含义。同样适用于null、false、true等
javascript 允许这样做吗?
a = {undefined : 1}
console.log(a[undefined])
[编辑] 我想我有点搞砸了 python 的习惯,但我实际上考虑过这个:
a = {[undefined] : 1}
console.log(a[undefined])
undefined
被转换为字符串(就像您使用此 synthax 传递的任何值一样)并且您最终定义了 "undefined"
prop
[/编辑]
如果是这样,是否所有浏览器都以相同的方式处理它,是否在标准中明确规定了?
[编辑]
注意:
这个问题与 'undefined' variable works as key to object with 'undefined' property name 略有不同,尽管根本原因和答案是相同的。
我在 undefined
似乎与 "is it the value undefined
or the string "undefined"
? " 不明确的上下文中询问,而链接的问题清楚地说明 "undefined"
作为道具名称并询问名称与关键字的冲突。
...而且我认为它可以帮助其他程序员使用 Python 或使用 json-like object/dict/hash synthax
的其他语言[/编辑]
Is this allowed in javascript ?
是的。
And if so, does all browsers handle it the same
是的。
is it explicitly specified in the standards ?
没有。
这只是 undefined
being converted to "undefined"
when used in string context 的结果:
console.log( ("" + undefined).toUpperCase() )
允许作为对象属性名称
访问a = {undefined : 1}
console.log(a.undefined)
对象 属性 名称始终是字符串。因此,无论您作为 属性 名称传递什么,都会转换为字符串。如果您传递 undefined,那么它将变为 "undefined"。所以这个:
let obj = { undefined: 0 }
obj[undefined]
与此相同
let obj = { undefined: 0 }
obj["undefined"]
undefined 在这种情况下没有特殊含义。同样适用于null、false、true等