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。

  • 相关阅读:
    sqlplus登录用户hang住
    ORACLE_19c用户密码登录失败的问题以及ORA-28040
    索引瘦身_oracle_11g
    Linux内存大页设置
    C++ 总体内容(2)
    C++ 总体内容(1)
    vector中erase的用法
    NGUI中的Tween的委托使用
    IOS Android支持中文与本地文件的读取写入
    Unity人工智能学习—确定性AI算法之追踪算法二
  • 原文地址:https://www.cnblogs.com/everlose/p/12965599.html
Copyright © 2011-2022 走看看