由于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)

如果您注意到,a
和c
的密钥与分配给对象的方式完全相同,则b
和d
似乎有键按升序排序。
任何想法为什么?