Heim  >  Artikel  >  Web-Frontend  >  javascript call和apply方法_javascript技巧

javascript call和apply方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:58:12971Durchsuche

例子如下:

复制代码 代码如下:

<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>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn