向Javascript对象添加键值对对数字和非数字键的行为有所不同

时间:2018-06-05 07:11:09

标签: javascript json

由于Javascript的古怪行为,我最近遇到了项目的错误。

考虑以下操作:



let a = {}
a['foo'] = "this"
a['bas'] = "that"
a['bar'] = "meh"

let b = {}
b[1] = "this"
b[3] = "that"
b[2] = "meh"

let c = {}
c[' ' + 1] = "this"
c[' ' + 3] = "that"
c[' ' + 2] = "meh"

let d = {}
d[1 + ''] = "this"
d[3 + ''] = "that"
d[2 + ''] = "meh"

console.log(a, b, c, d)




如果您注意到,ac的密钥与分配给对象的方式完全相同,则bd似乎有键按升序排序。

任何想法为什么?

0 个答案:

没有答案