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用于实现类似于继承的功能、稍后补充

  • 相关阅读:
    TP ajax
    TP分页
    TP表单验证
    TP数据删除
    TP数据查询
    TP【连接数据库配置及Model数据模型层】
    TP系统常量信息
    ThinkPHP中Session用法详解
    ThinkPHP部分内置函数
    element-ui select可搜索下拉框无法在IOS或Ipad调起小键盘输入法
  • 原文地址:https://www.cnblogs.com/web-coding/p/4711982.html
Copyright © 2011-2022 走看看