首先,slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组

在这里我们看第二个方法

1.在JS里Array是一个类 slice是此类里的一个方法
  那么使用此方法应该Array.prototype.slice这么去用
  slice从字面上的意思很容易理解就是截取(当然你不是英肓的话)
  这方法如何使用呢?
  arrayObj.slice(start, [end]) 很显然是截取数组的一部分

2.我们再看call
  call([thisObj[,arg1[arg2[[argN]]]]])
  thisObj是一个对象的方法
  arrg1~argN是参数
  那么Array.prototype.slice.call(arguments,1);这句话的意思就是说把调用方法的参数截取出来。
  如:
   function test(a,b,c,d)
   {
      var arg = Array.prototype.slice.call(arguments,1);
      alert(arg);
   }
   test("a","b","c","d");
   结果是:
 
  这样应该能理解了吧。
3. 然后我再看apply
   apply([thisObj[,argArray]])
   thisObj:
               可选项 将被用作当前对象的对象。
   argArray
              可选项 将被传递给该函数的参数数组。
4. 来一个实例重写setTimeout 我们知道Timeout不能传参数。(这个可真是不好)

  }

  window.setTimeout(test,1000,'fason',window,123,[5,6,7],new   Object());

5、实际参数在函数中我们可以使用 arguments 对象获得 (注:形参可通过 arguments.callee 获得),虽然 arguments 对象与数组形似,但仍不是真正意义上的数组。

  我们可以通过数组的 slice 方法将 arguments 对象转换成真正的数组。

  方法一:var args = Array.prototype.slice.call(arguments);

  方法二:var args = [].slice.call(arguments, 0);

  方法三:

  var args = [];
  for (var i = 1; i < arguments.length; i++) {
    args.push(arguments[i]);
  }

  注:一般的函数的 arguments.length 都在 10 以内,方法二有优势; 方法二的代码量上也比第一种少,至少可以减小一点字节

  下面附一个例子:

  function revse(){
    var args = Array.prototype.slice.call(arguments);
    newarr=[];
    for(var i=args.length-1;i>=0;i--){
      newarr.push(args[i]);
    }
     return args;
  }
  var s=revse('a','b','c');
  console.log(s);

最后,附个转成数组的通用函数

var toArray = function(s){
    try{
        return Array.prototype.slice.call(s);
    } catch(e){
            var arr = [];
            for(var i = 0,len = s.length; i < len; i++){
                //arr.push(s[i]);
                   arr[i] = s[i];  //据说这样比push快
            }
             return arr;
    }
}

详解 Array.prototype.slice.call(arguments)的更多相关文章

  1. Array.prototype.slice.call(arguments)

    Array.prototype.slice.call(arguments)能够将具有length属性的对象转化为数组, 可以理解为将arguments转化成一个数组对象,让它具有slice方法 如: ...

  2. Array.prototype.slice.call(arguments) 类数组转成真正的数组

    Array.prototype.slice.call(arguments)   我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数 ...

  3. 转对象(含length属性)成数组Array.prototype.slice.call(arguments)

    我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...

  4. js基础进阶--关于Array.prototype.slice.call(arguments) 的思考

    欢迎访问我的个人博客:http://www.xiaolongwu.cn Array.prototype.slice.call(arguments)的作用为:强制转化arguments为数组格式,一般出 ...

  5. 理解Array.prototype.slice.call(arguments)

    在很多时候经常看到Array.prototype.slice.call()方法,比如Array.prototype.slice.call(arguments),下面讲一下其原理: 1.基本讲解 1.在 ...

  6. Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)

    Array.prototype.push.apply(a,b) 时常看到在操作数组的时候有这样的写法: var a = [1,2,3]; var b = [4,5,6]; a.push.apply(a ...

  7. [转] 理解 JavaScript 中的 Array.prototype.slice.apply(arguments)

    假如你是一个 JavaScript 开发者,你可能见到过 Array.prototype.slice.apply(arguments) 这样的用法,然后你会问,这么写是什么意思呢? 这个语法其实不难理 ...

  8. javascript:Array.prototype.slice.call(arguments)

    我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...

  9. [转载]Array.prototype.slice.call(arguments,1)原理

    Array.prototype.slice.call(arguments,1)该语句涉及两个知识点. arguments是一个关键字,代表当前参数,在javascript中虽然arguments表面上 ...

随机推荐

  1. 1047. Student List for Course (25)

    Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course li ...

  2. [LintCode] House Robber II 打家劫舍之二

    After robbing those houses on that street, the thief has found himself a new place for his thievery ...

  3. Hibernate反向工程在javaweb下的操作配置

    1.在javaEE下新建项目,在WEB-INF的lib文件夹下添加所用到的jar包. 2.创建Hibernate 主配置文件 文件----新建----其他下的Hibernate目录,如图: 下一步,注 ...

  4. 30.编写一个Shape类,具有属性:周长和面积; 定义其子类三角形和矩形,分别具有求周长的方法。 定义主类E,在其main方法中创建三角形和矩形类的对象, 并赋给Shape类的对象a、b,使用对象a、b来测试其特性。

    package zuoye8; public abstract class Shape { private double zhouchang ; private double mianji ; pub ...

  5. android- 菜单

    选项菜单:menu_main.xml <?xml version="1.0" encoding="utf-8"?><menu xmlns:an ...

  6. JAVA 线程同步异步简单实例

    package test; public class testThread { public static void main(String[] args) { Example example = n ...

  7. ES6 有什么新东西

    ES6 有什么新东西? 你可能已经听说过 ECMAScript 6 (简称 ES6)了.ES6 是 Javascript 的下一个版本,它有很多很棒的新特性.这些特性复杂程度各不相同,但对于简单的脚本 ...

  8. 3_STL算法

    1.常用遍历算法1.1 for_each for_each(v1.begin(),v1.end(),show); void show(int &n) //回调函数的入口地址 { cout &l ...

  9. javascript 与jquery为每个p标签增加onclick方法

    <script type="text/javascript"> window.onload=function(){ var items=document.getElem ...

  10. 【iCore3 双核心板_FPGA】实验二十:基于FIFO的ARM+FPGA数据存取实验

    实验指导书及代码包下载: http://pan.baidu.com/s/1cmisnO iCore3 购买链接: https://item.taobao.com/item.htm?id=5242294 ...