一道类数组的面试题

问题

var obj = {
  2: '3',
  8: '9',
  length: 2,
  splice: Array.prototype.splice,
  push: Array.prototype.push
}

obj.push(1);
console.log(obj);
obj.push(2);
console.log(obj);

输出的结果是

Object {
2: 1
3: 2
8: "9"
length: 4
push: ƒ push()
splice: ƒ splice()
}

笔者扩展了一下

var obj = {
  2: '3',
  8: '9',
  length: 'abc',
  splice: Array.prototype.splice,
  push: Array.prototype.push
}

obj.push(5);
/*
Object { 0: 5
2: "3"
8: "9",
splice: ƒ,
push: ƒ }
*/

var obj = {
  6: '3',
  8: '9',
  length: 2,
  splice: Array.prototype.splice,
  push: Array.prototype.push
}

obj.push(5);

/*
Object { 2: 5
6: "3"
8: "9"
length: 3
push: ƒ
splice: ƒ }
*/

解释

push 这个方法应该是去识别类数组中的 length 属性的

  • 如果 length 属性存在并且为数字,那就设置 obj[length] 的值,并且 length + 1
  • length 不存在或者不为数字,那么从 obj[0] 开始设置,并且 length 被设置为 1.

ES 设计标准里说到

解释一下 ToLength 里调用了 ToInteger,ToInteger 里调用 ToNumber,ToNumber 的规则很多,但简言之无法被转化数字中的字符串返回 NaN 非数。ToUint32 里得到结果是非数,那就返回成 +0

If the grammar cannot interpret the String as an expansion of StringNumericLiteral, then the result of ToNumber is NaN.

翻译:如果此文法无法将字符串解释为「字符串数值常量」的扩展,那么 ToNumber 的结果为 NaN。

posted @ 2020-05-26 14:52  Ever-Lose  阅读(176)  评论(0编辑  收藏  举报