• 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 oracle命令行窗口命令上下翻阅
    oracle 转移表空间
    perl字符集处理
    Perl解析JSON数据精解
    处理外壳PROC
    FileIsExe
    写壳前的设计
    SEH结构化异常处理03
    SEH结构化异常处理02
    博客首记
  • 原文地址:https://www.cnblogs.com/web-coding/p/4711982.html
Copyright © 2020-2023  润新知