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。

  • 相关阅读:
    异常处理 try catch throw(C++)
    Kubernetes轻量级日志收集系统LokiStack
    第一章.java
    第四章.选择结构(二)
    java语法
    第三章if选择结构
    第二章.数据类型变量名和运算符
    【转载】跳槽七诫
    【转载】修改shell终端提示信息
    ubuntu11.10面板上输入法图标消失解决办法
  • 原文地址:https://www.cnblogs.com/everlose/p/12965599.html
Copyright © 2011-2022 走看看