您的位置:首页 > 移动开发

js中apply()和call()方法的使用

2016-05-19 14:51 573 查看

1.apply()方法

apply方法能劫持另外一个对象的方法,继承另外一个对象的属性。

  Function.apply(obj,args)方法能接收两个参数

obj:这个对象将代替Function类里this对象

args:这个是数组,它将作为参数传给Function(args-->arguments)

使用apply()方法时可以直接将当前函数的arguments对象作为apply的第二个参数传入

(1)apply()方法可实现继承

eg:function Person(name,age){ //定义一个类,人类

this.name=name; //名字

this.age=age; //年龄

this.sayhello=function(){console.log("hello")};

}

function
Print(){ //显示类的属性

this.funcName="Print";

this.show=function(){

var msg=[];

for(var key in this){

if(typeof(this[key])!="function"){

msg.push([key,":",this[key]].join(""));

}

}

console.log(msg.join(" "));

};

}

function Student(name,age,grade,school){ //学生类

Person.apply(this,arguments);

Print.apply(this,arguments);

this.grade=grade; //年级

this.school=school; //学校

}

var p1=new Person("jake",10);

p1.sayhello(); //hello

var s1=new Student("tom",13,6,"清华小学");

s1.show(); //name:tom age:13 funcName:Print grade:6 school:清华小学

s1.sayhello(); //hello

console.log(s1.funcName); //Print

学生类本来不具备任何方法,但是在Person.apply(this,arguments)后,它就具备了Person类的sayhello方法和所有属性。在Print.apply(this,arguments)后就自动得到了show()方法。

(2) apply()方法可以将参数数组默认的转换为参数列表

我们先从Math.max()函数说起,Math.max后面可以接任意个参数,最后返回所有参数中的最大值。

eg: Math.max(5,8) //8

  Math.max(5,7,9,3,1,6) //9

但是在很多情况下,我们需要找出数组中最大的元素。

var arr=[5,7,9,1]

console.log(Math.max(arr)) // NaN 这个方法无法实现

需要按如下所示的方法才可以实现:

eg:function getMax(arr){

var arrLen=arr.length;

for(var i=0,ret=arr[0];i<arrLen;i++){

ret=Math.max(ret,arr[i]);

}

return ret;

}

这样写麻烦而且低效。如果用 apply呢,看代码:

function getMax2(arr){

return Math.max.apply(Math,arr);

}

两段代码达到了同样的目的,但是getMax2却更高效简洁。这里主要是由于apply可以将一个数组默认的转换为一个参数列表(即可以将数组[param1,param2,param3] 转换为 param1,param2,param3) ,如果让我们用程序来实现将数组的每一个项来转换为参数列表,像getMax方法一样会比较麻烦。借助apply的这点特性,就有了更加高效的解决方法如getMax2方法所示。

再比如数组的push方法。

var arr1=[1,3,4];

var arr2=[3,4,5];

如果我们要把 arr2展开,然后一个一个追加到arr1中去,最后让arr1=[1,3,4,3,4,5]

arr1.push(arr2)显然是不行的。
因为这样做会得到[1,3,4,[3,4,5]]

我们只能用一个循环去一个一个的push(当然也可以用arr1.concat(arr2),但是concat方法并不改变arr1本身,而是创建了一个新的数组)

var arrLen=arr2.length

for(var i=0;i<arrLen;i++){

arr1.push(arr2[i]);

}

自从有了Apply,事情就变得如此简单

Array.prototype.push.apply(arr1,arr2) 或者 arr1.push.apply(arr1,arr2);

2.call()方法

call()方法与apply()方法的功能是一样的,只不过是参数列表不一样。如 func.call(func1,var1,var2,var3)对应的apply写法为:func.apply(func1,[var1,var2,var3])

Function.call(obj,[param1[,param2[,…[,paramN]]]])
obj:这个对象将代替Function类里this对象
params:这个是一个参数列表

eg:function Animal(){

  this.name = "Animal";
  this.showName = function(){
  console.log(this.name); }
}

function Cat(){
  this.name = "Cat";
  this.sayWorld=function(){
    console.log("hello world")};
  this.sayName=function(){
    console.log(this.name);}
}
var animal = new Animal();
var cat = new Cat();
Cat.call(animal);
animal.showName.call(cat); // Cat
animal.sayWorld(); //hello world
animal.sayName(); // Cat

3.什么情况下用apply()什么情况下用call()

  在给对象参数的情况下,如果参数的形式是数组的时候,比如apply示例里面传递了参数arguments,这个参数是数组类型,并且在调用Person的时候参数的列表是对应一致的(也就是Person和Student的参数列表前两位是一致的) 就可以采用 apply , 如果我的Person的参数列表是这样的(age,name),而Student的参数列表是(name,age,grade),这样就可以用call来实现了,也就是直接指定参数列表对应值的位置(Person.call(this,age,name,grade));

  学习自:http://www.cnblogs.com/delin/archive/2010/06/17/1759695.html

    http://www.cnblogs.com/KeenLeung/archive/2012/11/19/2778229.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: