The difference between call and apply and 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.

A major feature of JavaScript is that functions have "context of definition" and "runtime context" and "context can be changed."

Apply (): invokes the function as a method of the specified object, passed to it by the specified array of arguments
function.apply (thisobj, args) or function.apply (thisobj, args)

1. Thisobj is the object that calls function, Thisobj is this in the body, and if the argument is null the global object is used
2. Returns the return value of the calling function

bind (): primary role: Binds a function to an object, returns a new function, invokes the method with an optional specified argument, as a method of the specified objectGrammar Fun.bind (thisarg[, arg1[, arg2[, ...])
Function.bind (a) or function.bind (A, args ...);

1. A object to be bound on the function
2. Args ... 0 or more function values to bind to the function
3. Returns a new function that will be called as a method and passed to the args parameter

Bind is a new way to ES5
The reference is similar to call or apply
does not execute the corresponding function, call or apply automatically executes the corresponding function
Returns a reference to a function

call (): similar to apply, the function is called as a method of the specified object, and is passed to the specified parameter
Function.call (thisobj, args ...)

1. Thisobj is the object that calls function, Thisobj is this in the body, and if the argument is null the global object is used
2. Returns the return value of the calling function

The difference between apply and call:

<script>functionPrint (a,b,c,d) {alert (a+ B + C +d); }                    functionExample (a,b,c,d) {//when the parameters are clear, the call is generally usedPrint.call ( This, a,b,c,d); //Encapsulation ArrayPrint.apply ( This, [a,b,c,d])//The JavaScript method itself has a arguments array that can be combined with apply when the parameters are ambiguous argumentsPrint.apply ( This, arguments)} Example ("Heart", "Want", "thing", "Cheng")             </script>
var function (Arg1, arg2) {     }; can be invoked as follows:func.call (This, arg1, arg2); Func.apply (  this, [Arg1, arg2])

Three different demo:

<script type= "Text/javascript" >varobj ={num:888,            }; varFoo ={getnum:function() {                    return  This. Num; }} console.log (Foo.getNum.bind (obj) ()); //888Console.log (Foo.getNum.call (obj));//888Console.log (foo.getNum.apply (obj));//888            //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. </script>

The difference between call and apply and bind

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.