JavaScript function bind call Apply Difference

Source: Internet
Author: User

1. Apply Calll

  In JavaScript, call and apply are meant to change the runtime context of a function, in other words, to change the direction of this within the function.

  

Here we have a new object banana object But without say method we can use the Apple say method via call or apply.

Pass in the banana directly to make this point pointing to the banana object.

Apply and call are essentially exactly the same as the parameters that are being received . The arguments to apply (the arguments are indeterminate) are placed inside an array .

  

Where this is the context you want to specify . He can be any JavaScript object ,

  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.

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.

2. Array Append

  

  

C: How to use a class (pseudo) array

var domnodes = Array.prototype.slice.call (document.getElementsByTagName ("*"));

There is an object structure called a pseudo-array in JavaScript that is particularly arguments objects. As well as calling getElementsByTagName Document.childnodes and the like,

They return a NodeList object that belongs to a pseudo-array. You cannot use the push and pop methods under array. But we can convert the Array.prototype.slice.call to a real array with the Length property

Object so that domnodes can be used for all methods below the array.

3. In-depth understanding of the Apply call

  

  The above method can solve the problem but the number of parameters passed in can not be determined when the above method is invalid, this time will be used to call and apply but because of the number of arguments is not determined

So using apply is the best.

  

The next requirement is to add a "(app)" prefix to each log message. Now we're going to think that arguments is a pseudo-array. We first want to convert the array.

The Array.prototype.slice.call is converted to a standard array. The Unshift () of the array is used.

  

4. JavaScript bind

  The bind () method is similar to apply and call and can also change the direction of this in the function body.

The MDN explanation is that thebind () method creates a new function called a binding function that, when called, binds the binding function to the first parameter of the bind () method when it is created, and does not change any call to this.

The second and later parameters of the incoming bind () method, plus the arguments of the binding function itself, call the original function as a parameter of the original function in order.

In JavaScript, multiple bind () is invalid. For a deeper reason, the implementation of BIND () is equivalent to using a function to wrap a call/apply inside, and the second bind () is equivalent to wrapping the first bind (), so the second bind is not valid.

  

Three are output 81 but the bind () method is followed by a parenthesis. Describes the use of the Bing () method when you want to change the context and not execute it immediately but instead call it execution

The use of call apply will be performed immediately .

    • Apply, call, and bind are all pointers to the This object that is used to change the function;
    • Apply, call, bind the first parameter is the object that this is to point to, that is, the context you want to specify;
    • Apply, call, bind three can use the following parameters to pass the parameter;
    • Bind is to return the corresponding function, which is convenient to call later; apply, call is called immediately.
5. A detailed explanation of bind

The simplest use of bind () is to create a function that allows the function to have the same this value regardless of how it is called.

   

  

JavaScript function bind call Apply Difference

Related Article

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.