Talking about the difference between call apply Bind

Source: Internet
Author: User
<span id="Label3"></p><p><p>The usage of these three methods is very similar, binding the function to the context, which is used to change the direction of this in the Function. As an example:</p></p><span class="code_copy"><span class="code_copy"></span></span><pre><pre>var zlw = { name: "zlw", sayhello:function (age) { console.log ("hello, i am", this.name + "+ age" years Old ");} ; var xlj = { <span class="objectBox objectBox-text ">hello, I am zlw years</span> old</pre></pre><pre><pre></pre></pre><span class="code_copy"><span class="code_copy"></span></span><p><p>Here's How to use the call and apply Methods:</p></p><pre><pre><span class="objectBox objectBox-text "><span class="objectBox objectBox-text ">hello, I am XLJ years old<br></span></span></pre></pre><p><p>The results are the Same. From the writing we can see the similarities and differences between the TWO. the <strong>same is because the first parameter is the context to bind to, and the arguments that follow are passed to the function that called the Method. The difference is that the parameters that the call method passes to the calling function are listed individually, and apply is written in the Array. </strong></p></p><p><p>Let's look at the use of the bind method:</p></p><pre><pre>Zlw.sayHello.bind (xlj, 24) (); hello, I am xlj years oldzlw.sayHello.bind (xlj, [24]) (); hello, I am XLJ years old</pre></pre><p><p><strong>The arguments that the bind method passes to the calling function can be listed individually or in an array. The most important difference between the <span style="color: #99cc00">bind method and call, apply is that the former returns a binding context function, and the two are directly executing the Function. </span>for this reason, the above code can also be written like this:</strong></p></p><pre><pre>Zlw.sayHello.bind (xlj) (24); hello, I am xlj years oldzlw.sayHello.bind (xlj) ([24]); hello, I am XLJ years old</pre></pre><p><p>The Bind method can also write <span class="code">fn.bind (obj, arg1) (arg2). </span></p></p><p><p>Summarize the use of bind in one Sentence: <strong>the method creates a new function, called a binding function, and the binding function takes the first parameter of the Bind method as this when it is Created. The second and later parameters of the incoming bind method plus the arguments of the binding function itself are invoked in order as arguments to the original Function. </strong></p></p><p><p>Talking about the difference between call apply Bind</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.