Maison  >  Article  >  interface Web  >  javascript call和apply方法_javascript技巧

javascript call和apply方法_javascript技巧

WBOY
WBOYoriginal
2016-05-16 18:58:12971parcourir

例子如下:

复制代码 代码如下:

<script> <BR>/** <BR>*动物 <BR>*/ <BR>function Animal(){ <BR>this.name='Amimal'; <BR>this.showName=function(){ <BR>alert(this.name); <BR>}; <BR>} <BR>/* <BR>*猫 <BR>*/ <BR>function Cat(){ <BR>this.name='cat'; <BR>} <BR>var animal=new Animal;//创建动物对象 <BR>var cat=new Cat;//创建猫对象 <BR>animal.showName.call(cat,'','');//输出cat,说明showName函数的当前this已经变为cat了 <BR>animal.showName.apply(cat,[]);//输出cat <BR>//call函数和apply函数的区别是call 的语法是function.call(obj,param1,param2……);applay的语法是function.call(obj,[]/*params[]参数数组*/); <BR></script>

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn