zoukankan      html  css  js  c++  java
  • 一道类数组的面试题

    问题

    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。

  • 相关阅读:
    进制转换
    01背包基础
    6-14 Inspector s Dilemma uva12118(欧拉道路)
    9-4 Unidirectional TSP uva116 (DP)
    8-4 奖品的价值 uva11491(贪心)
    9-1 A Spy in the Metro uva1025 城市里的间谍 (DP)
    8-3 Bits Equalizer uva12545
    8-2 Party Games uva1610 (贪心)
    自动发邮件功能
    窗口截图.py
  • 原文地址:https://www.cnblogs.com/everlose/p/12965599.html
Copyright © 2011-2022 走看看