两个方法都是通过改变this指针来调整对象调用,区别是apply传递两个参数,第一个是被调整的方法,第二个是详细列举的参数数组(也可以是arguments),call的参数不定,第一个是被调用的方法,第二个之后都是逐个参数,需要一一列举。
function add(c,d){ return this.a + this.b + c + d; } var s = {a:1, b:2}; console.log(add.call(s,3,4)); // 1+2+3+4 = 10 console.log(add.apply(s,[5,6])); // 1+2+5+6 = 14
arguments只存在于function,是函数里参数集合,使用如下:
function add(c,d){ return c + d; } function sub(a,b){ // return this.a+this.b; console.log(add.apply(this,arguments)); } var sub=sub(1,2);
简单介绍:
http://uule.iteye.com/blog/1158829