Home > Web Front-end > JS Tutorial > body text

javascript call and apply methods_javascript skills

WBOY
Release: 2016-05-16 18:58:12
Original
970 people have browsed it

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>

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!