js中call和apply

一、定义:数组

 apply:方法能劫持另一个对象的方法,继承另一个对象的属性.app

   如: Function.apply(obj,args),obj:这个对象将代替Function类里this对象性能

                 args:这个是数组,它将做为参数传给Function。this

call:和apply的意思同样,只不过是参数列表不同. spa

   如:Function.call(obj,[param1[,param2[,…[,paramN]]]]),obj:这个对象将代替Function类里this对象prototype

                              params:这个是一个参数列表对象

二、示例: blog

首先, Person.apply(this,arguments)中this指向var student,也就是student窃取了Person方法,用student的参数去执行命名为student的Person方法。在Student与Person参数顺序不一致时用call直接指定对应参数(如Person(age,name)能够用Person.call(this,age,name,grade)将student中参数顺序更正)继承

三、其余用法io

能够将数组转为参数列表;如var max=Math.max.apply(null,array),Array.prototype.push.apply(arr1,arr2)(但性能不如concat)

相关文章
相关标签/搜索