The use of Apply,call

Source: Internet
Author: User
<span id="Label3"></p><span style="font-size: 15px;"><span style="font-size: 15px;">in javascript, call and apply are meant to change the context in which a function is run, in other words, to change the direction of this within the function Body. </span></span><span style="font-size: 15px;"><span style="font-size: 15px;">A major feature of JavaScript is the notion that a function has a " <span style="color: #ff6600;">context of definition" and a " <span style="color: #ff6600;">runtime context" and a " <span style="color: #ff6600;">context can be changed." </span></span></span></span></span><span style="font-size: 15px;"><span style="font-size: 15px;">first, a chestnut:</span></span> <pre><pre><span style="color: #0000ff;">function</span><span style="color: #000000;"></span>=<span style="color: #000000;"> { </span>"red"<span style="color: #000000;">, </span><span style="color: #0000ff;">function</span><span style="color: #000000;">() { console.log (</span> <span style="color: #0000ff;"> </span>this<span style="color: #000000;">. color); </span> <span style="color: #0000ff;">var</span> <span style="color: #0000ff;">New</span> <span style="color: #000000;">Fruits;apple.say (); </span><span style="color: #008000;"><br></span></pre></pre><p><p>  </p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">now there is a new object banana</span> <span style="color: #0000ff;">var</span> banana=<span style="color: #000000;">{color:</span>"yellow"<span style="color: #000000;">}apple.say.call (banana);</span> <span style="color: #008000;">//</span> <span style="color: #008000;">my color is yellow</span>apple.say.apply (banana); <span style="color: #008000;">//</span> <span style="color: #008000;">my color is yellow</span></pre></pre><p><p><span style="font-family: 黑体; font-size: 15px;">The effect of this is that Apple's object is pointing to the banana object after invoking the call or apply Method.</span></p></p><span style="font-family: 黑体; font-size: 15px;"><span style="font-family: 黑体; font-size: 15px;">the difference between apply,call is that</span></span><p><p><span style="font-family: 黑体; font-size: 15px;">  </span></p></p><code class="javascript keyword"><code class="javascript keyword">var</code></code> <code class="javascript plain"><code class="javascript plain">func = </code></code><code class="javascript keyword"><code class="javascript keyword">function</code></code><code class="javascript plain"><code class="javascript plain">(arg1, arg2) {</code></code><code class="javascript spaces"><code class="javascript spaces">    </code></code><code class="javascript plain"><code class="javascript plain">};</code></code><p><p><span style="font-size: 15px;">You can invoke the following method:</span></p></p><p><p></p></p><p><p><span style="font-size: 15px;">Func.call (this,arg1,arg2);</span></p></p><p><p><span style="font-size: 15px;">Func.apply (this,[arg1,arg2]);</span></p></p><p><p></p></p><p><p><span style="font-size: 15px;">Where this is the context you want to specify, he can be any JavaScript object (everything in javascript), call needs to <span style="color: #ff6600;">pass parameters in order, and apply is to put the <span style="color: #ff6600;">parameters in the Array. </span></span></span></p></p><p><p></p></p><span style="font-size: 15px;"><span style="font-size: 15px;">in JavaScript, the number of arguments for a function is not fixed, so if you want to apply the condition, call is used when your parameters are clearly known. </span></span><p><p></p></p><span style="font-size: 15px;"><span style="font-size: 15px;">use Apply when unsure, and then pass in the parameter push into the ARRAY. When the number of arguments is indeterminate, the function can also traverse all the arguments by arguments the Array. </span></span><p><p></p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter"></td> <td class="code"></td> </tr> </tbody> </table><p><p>The use of Apply,call</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.