Home  >  Article  >  Web Front-end  >  javascript call and apply methods_javascript skills

javascript call and apply methods_javascript skills

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

The example is as follows:

Copy code The code is as follows:

<script> <br>/* *<br>*Animals <br>*/ <br>function Animal(){ <br>this.name='Amimal'; <br>this.showName=function(){ <br>alert(this.name); <br>} ; <br>} <br>/* <br>*cat<br>*/ <br>function Cat(){ <br>this.name='cat'; <br>} <br>var animal=new Animal;//Create animal object<br>var cat=new Cat;//Create cat object<br>animal.showName.call(cat,'','');//Output cat, indicating the current this of the showName function It has become cat <br>animal.showName.apply(cat,[]);//Output cat <br>//The difference between the call function and the apply function is that the syntax of call is function.call(obj,param1,param2 ...); The syntax of appplay is function.call(obj,[]/*params[]parameter array*/); <br></script>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn