zoukankan      html  css  js  c++  java
  • Array.prototype.slice.call(arguments)

    Array.prototype.slice.call(arguments)

    话说可以通过call()来让arguments来继承array的slice()方法。

    可是为何使用arguments.slice(1)会报错,

    而这样使用Array.prototype.slice.call(arguments,1)就可以正常。

    function name(){
    Array.prototype.slice.call(arguments);
    console.log(arguments.slice(1));
    }
    name("some","thing");

    结果:TypeError: arguments.slice is not a function

    function name(){
    console.log(Array.prototype.slice.call(arguments,1));
    }
    name("some","thing");

    结果为: ["thing"]

    mdn给出的把arguments转换成array对象时,做了一个赋值:var args = Array.prototype.slice.call(arguments);

    function name(){
    var arg=Array.prototype.slice.call(arguments);
    console.log(arg instanceof Array);
    console.log(arg.slice(1));
    }
    name("some","thing");

    结果是: true
                ["thing"]

    所以arg在这里才是arguments转换知之后的array对象。

    那我们需要研究一下call()返回了什么?

    //备注

    array.slice();用于选出数组中制定元素,返回一个新的数组对象。

    call();关于call用于实现类似于继承的功能、稍后补充

  • 相关阅读:
    一、linux 挂起进程 nohup
    1.C#窗体和控件
    C#笔记——5.迭代器
    C#笔记——4.集合
    设计模式——3.观察者模式
    设计模式——2.策略模式
    Code基础——1.数据结构
    设计模式——1.模板方法
    C#笔记——3.泛型
    C#笔记——2.委托
  • 原文地址:https://www.cnblogs.com/web-coding/p/4711982.html
Copyright © 2011-2022 走看看