Apply and call in JavaScript

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong><span id="cjabiokabamngnbigeeibddnihgllkggmousedown-target-element">1. Method definition</span></strong></p></p><p><p>call <strong>method:</strong><br>Syntax: call ([thisobj[,arg1[, arg2[, [,. argN]]])<br>Definition: invokes one method of an object, replacing the current object with another Object.<br>Description<br>The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by thisobj.<br>If the thisobj parameter is not provided, then the Global object is used as the thisobj.<br><br><strong>Apply Method:</strong><br>Syntax: Apply ([thisobj[,argarray]])<br>Definition: a method of applying an object that replaces the current object with another Object.<br>Description<br>If Argarray is not a valid array or is not a arguments object, it will result in a TypeError.<br>If none of the Argarray and thisobj parameters are provided, then the Global object is used as a thisobj and cannot be passed any Parameters.</p></p><p><p></p></p><p><p><strong>2. Common examples</strong></p></p><p><p>A</p></p>JS Code <ol class="dp-j" start="1"> <ol class="dp-j" start="1"> <li>function Add (a, b)</li> <li>{</li> <li>Alert (a+b);</li> <li>}</li> <li id="">function Sub (a, b)</li> <li id="">{</li> <li>Alert (a-b);</li> <li id="">}</li> <li></li> <li>Add.call (sub,<span class="number">3,<span class="number">1); </span></span></li> </ol> </ol><p><p>The meaning of this example is to replace sub,add.call (sub,3,1) = = Add (3,1) with add, so the result is: alert (4); Note: the function in JS is actually an object, and the function name is a reference to a function object.</p></p><p id=""><p id=""></p></p><p><p>B</p></p>JS Code <ol class="dp-j" start="1"> <li><li>function Animal () {</li></li> <li><li><span class="keyword">this.name = <span class="string">"Animal"; </span></span></li></li> <li><li><span class="keyword">This.showname = function () {</span></li></li> <li><li>Alert (<span class="keyword">this.name); </span></li></li> <li id=""><li id="">}</li></li> <li><li>}</li></li> <li><li></li></li> <li><li>function Cat () {</li></li> <li><li><span class="keyword">this.name = <span class="string">"Cat"; </span></span></li></li> <li><li>}</li></li> <li><li></li></li> <li><li>var animal = <span class="keyword">new Animal (); </span></li></li> <li><li>var cat = <span class="keyword">New<span id=""> cat (); </span></span></li></li> <li><li></li></li> <li><li><span class="comment">Using the call or Apply method, the ShowName () method that originally belonged to the animal object is given to the object cat.</span></li></li> <li><li><span class="comment">The input result is "Cat"</span></li></li> <li><li>Animal.showName.call (cat,<span class="string">","); </span></li></li> <li><li><span class="comment">Animal.showName.apply (cat,[]);</span></li></li> </ol><p><p>Call means to put the animal method on the cat, the original cat is no showname () method, now is to put animal showname () method on the cat to execute, so this.name should be cat</p></p><p><p></p></p><p id=""><p id="">C. Implementing inheritance</p></p>JS Code <ol class="dp-j" start="1"> <li id=""><li id="">function Animal (name) {</li></li> <li><li><span id=""><span class="keyword">this.name = name; </span></span></li></li> <li><li><span class="keyword">This.showname = function () {</span></li></li> <li><li>Alert (<span class="keyword">this.name); </span></li></li> <li><li>}</li></li> <li><li>}</li></li> <li><li></li></li> <li id=""><li id="">function Cat (name) {</li></li> <li><li>Animal.call (this<span class="keyword">, name); </span></li></li> <li><li>}</li></li> <li><li></li></li> <li><li>var cat = <span class="keyword">New Cat (<span class="string">"Black cat"); </span></span></li></li> <li><li>Cat.showname ();</li></li> </ol><p><p>Animal.call (this) means that the Animal object is used instead of the this object, so there is no Animal of all the properties and methods in cat, and the Cat object can directly invoke the Animal method and Properties.</p></p><p><p></p></p><p><p>d, Multiple Inheritance</p></p>JS Code <ol class="dp-j" start="1"> <li><li>function Class10 ()</li></li> <li><li>{</li></li> <li><li><span class="keyword">this.showsub = function (a, b)</span></li></li> <li><li>{</li></li> <li><li>Alert (a-b);</li></li> <li><li>}</li></li> <li><li>}</li></li> <li><li></li></li> <li><li>function Class11 ()</li></li> <li><li>{</li></li> <li><li><span class="keyword">this.showadd = function (a, b)</span></li></li> <li><li>{</li></li> <li><li>Alert (a+b);</li></li> <li><li>}</li></li> <li><li>}</li></li> <li><li></li></li> <li><li>function Class2 ()</li></li> <li><li>{</li></li> <li><li>Class10.call (this<span class="keyword">); </span></li></li> <li><li>Class11.call (this<span class="keyword">); </span></li></li> <li><li>}</li></li> </ol><p><p>It's easy to implement multiple inheritance with two call<br><br></p></p><p><p>Apply and call in JavaScript</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.