合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 题目 ~~~ var obj = { '2': 3, '3': 4, 'length': 2, 'splice': Array.prototype.splice, 'push': Array.prototype.push } obj.push(1) obj.push(2) console.log(obj) ~~~ ![](https://img.kancloud.cn/84/e6/84e66e57a4c496bad3674cb24edffc03_400x311.png) ## 解释 > 结果最后变为一个稀疏数组[,,1,2]。 > 因为在对象中加入splice属性方法,和length属性后。这个对象变成一个类数组。 > 属性名称可转换为数字时,会映射成为索引下标。 所以对象实际可这样描述:[,,3,4], 然后继续执行length 属性复制,length 原来值为4,length:2 语句将数组截断成为长度为2的数组。此时数组为[,,]。 接着push(1)、push (2)。数组追加两个值,[,,1,2]。 ## 摘自 [输出以下代码执行的结果并解释为什么](https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/76)