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

    slice(start[, end])方法

    概念:通过索引位置获取新的数组,该方法不会修改原数组,返回一个新的子数组,常用于数组的截取

    call(thisArg, arg1, arg2,arg3)方法

    概念:一个对象调用另一个对象的方法,第二个参数为列表列表形式,这是与Apply()第二个参数为数组形式不同的地方

    Array.prototype.slice.call() 能将具有length属性的对象转成数组,比如类数组(arguments,NodeList)、字符串(String)

    Array.prototype.slice.call(arguments, index) 从第index个开始转换成数组,arguments继承了数组的原型方法中的slice方法

    应用场景(一):将函数的实际参数转化为数组

    代码实现

    1、Array.prototype.slice.call(arguments)
    2、[].slice.call(arguments,0)
    3var args = [];
         for(var i=1;i<arguments.length;i++){
             args.push(arguments[i])
         }
     function test(){
           console.log(arguments)//{0:"a",1:"b",2:"c",length:3}
           console.log(Array.prototype.slice.call(arguments));//[a,b,c],将test的实际参数转化为了数组
        };
     test("a","b","c");

    应用场景(二):将字符串,有length属性对象转化为数组,将数字,布尔值,普通对象转化为空数组

    代码实现

    console.log(Array.prototype.slice.call('string')) //["s", "t", "r", "i", "n", "g"]
    console.log(Array.prototype.slice.call(true)) //[]
    console.log(Array.prototype.slice.call(1)) //[]
    console.log(Array.prototype.slice.call({0:'obj'})) //[]
    console.log(Array.prototype.slice.call({0: 'obj1', 1: 'obj2', 2: 'obj3', length: 2}));  // ["obj1", "obj2"]
    
    
  • 相关阅读:
    在 ASP.NET 2.0 中上载文件
    ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器<from Copying>
    aspnetupload 上传组件在VS2008中使用详细说明
    基于asp.net 的文件上传和下载~~~转
    设置竖直的分割符【使用div】 (根据屏幕的大小自适应)
    分隔线
    UGUI事件系统
    U3D音频系统
    Unity启动事件-监听:InitializeOnLoad
    VS生成桌面应用程序
  • 原文地址:https://www.cnblogs.com/zhangyaolan/p/11157475.html
Copyright © 2011-2022 走看看