zoukankan      html  css  js  c++  java
  • 关于[].slice.call(arguments, 1) 的思考

    var a = function(f){
        console.log(arguments);
    }
    
    a('show', [12,3,4,55]);
    
    结果:['show', Array[4]]
    • 测试2
    var a = function(f){
        console.log([].slice.call(arguments, 1));
    }
    
    a('show', [12,3,4,55]);
    
    结果:[Array[4]]
    • 测试3
    var a = function(f){
        console.log(arguments.slice(1));
    }
    
    a('show', [12,3,4,55]);
    
    结果:报错!!! **arguments.slice is not a function(...)**

    此时就心中产生了疑惑了,为啥在测试一中打印出来的arguments 是个数组的东西啊,为啥会提示没有slice这个方法呢?带着疑问我去请教下了队伍里的大牛,得到了下面的答案:

    (function() {
        console.log(arguments instanceof Array)
    })();
    
    结果:false

    arguments 并非数组,只是访问单个参数的方式与访问数组元素的方式相同。因此在使用slice方法的时候,需要用类似[].slice.call(arguments, 1) 的这种方式去调用,至此,关于这条语句引发的思考也就此结束了。

  • 相关阅读:
    软件工程第三次作业
    软件工程第一次作业
    软件工程第0次作业
    第2次作业
    第1次作业
    第0次作业
    软件工程第四次作业 石墨文档IOS
    软件工程第三次作业
    软件工程第一次作业
    第零次作业
  • 原文地址:https://www.cnblogs.com/thaiwx/p/7544394.html
Copyright © 2011-2022 走看看