In Javascript, apply call bind

Source: Internet
Author: User

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.

Let's start with an example:

function="Red"function() {console.log (this. Color );}} var New  //My color is red

But if we have an object banana= {color: "Yellow"}, we don't want to redefine it say method, then we can use call or apply with Apple's say method:

Banana ="Yellow"//my color is yellow//my color is yellow

So we re-used the fruits say method.

So, you can see that call and apply are to change this dynamically, when an object does not have a method (this pest banana no say method), but the others have (this pest Apple has say method), We can use call or apply to manipulate other object methods.

the difference between apply and call

For both apply and call, the function is exactly the same, except that the parameters are accepted in a different way. For example, there is a function defined as follows:

Func.call (This, arg1, arg2); Func.apply (This, [Arg1, arg2])

Where this is the context you want to specify, he can be any JavaScript object (everything in JavaScript), call needs to pass parameters in order, and apply is to put the parameters in the array.

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.

Some common uses:
1. Append between arrays

var array1 = [N, "foo", {name "Joe"}, -2458]; var array2 = ["Doe", 555, +]; Array.prototype.push.apply (Array1, array2); /* */

2. Get the maximum and minimum values in the array

var numbers = [5, 458, -215 ]; var // 458 // 458

Number this array itself does not have the Max method, but with Math, we can use call or apply its methods.

3. Verify that it is an array (provided the ToString () method has not been rewritten)

function IsArray (obj) {    return Object.prototype.toString.call (obj) = = = ' [Object Array] ' ;}

4. Class (pseudo) array using array method

There is an object structure called a pseudo-array in JavaScript. In particular, there are arguments objects, as well as getElementsByTagName, document.childnodes and other return values, which return nodelist objects that belong to a pseudo-array. You cannot use push, pop, and so on under the array.

But we can convert the object with the length property to the real array by Array.prototype.slice.call, so that domnodes can apply all the methods under the array.

Apply, call, bind comparison

So what are the similarities and differences between apply, call, and bind? When to use apply, call, and when to use bind. Simple one of the chestnuts:

var obj =bayi,}; var foo =function() {returnthis//  Bayi//Bayi//Bayi

All three outputs are 81, but note that with the bind () method, he has more pairs of parentheses behind him.

That is, the difference is that the bind () method is used when you want to change the context and not execute immediately, but rather when the callback executes. The Apply/call executes the function immediately.

Let's summarize:

    • 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.

In Javascript, apply call bind

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.