Illustrate the similarities and differences between call, apply, bind and a variety of practical application demonstrations

Source: Internet
Author: User

I. The similarities and differences between call, apply and bind

In JavaScript, functions can change their this point in 3 ways, which are call, apply, bind. 3 of them are very similar, but there are differences. The table below can be very intuitive to see the difference between the three

Method Whether the function is executed directly Parameters passed in Invocation mode
Pager Is

(Context,arg1,arg2,arg3 ...)

The second argument is followed by an argument

Function.call (Context,arg1,arg2,arg3 ...)

Apply Is

(Context,[arg1,arg2,arg3 ...])

The second argument must be an array

Function.apply (Context,[arg1,arg2,arg3 ...])

Bind Whether

(context)

Only one parameter

var newfunction = function.bind (context);

Newfunction (Arg1,arg2,arg3 ...)

Second, example:

1. Call

    var a = {x:1};    var B = function (Y, z) {        console.log (this.x + y + z)    };    B.call (A, 3, 4);//The This of B (that is, the context of execution of B) is changed to a, so this.x is 1, and the second parameter is passed to the B argument.

2. Apply

   var a = {x:1};    var B = function (arr) {        console.log (this.x + arr[0] + arr[1])    };    B.call (A, [3, 4]);//The This of B (that is, the context of execution of B) is changed to a, the second argument is an array

3. Bind

    var a = {x:1};    var B = function (Y, z) {        console.log (this.x + y + z)    };    var newb = B.bind (a);//The This of B (that is, the context of execution of B) is changed to a, thus generating a new function, and the function is not executed immediately.    newb (3, 4);//function is only executed at this time

Iii. Common Scenarios

1. Append between arrays

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

2. Get the maximum and minimum values in the array

    var numbers = [5, 458, -215];     var maxinnumbers = Math.max.apply (Math, numbers);  // 458
View Code

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] ';    } 
View Code

4. Class (pseudo) array using array method

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

5. Sum of Numbers

    function sum () {        var numbersum = Array.prototype.reduce.apply (arguments, [function  (prev, Next) {            return prev + next;        }]);        Console.log (numbersum);    }    SUM (1, 2, 3);
View Code

Note: The above 1-4 of the use of the scene from, interested students can go to learn more: Https://github.com/chokcoco/apply-call-bind/tree/master

Illustrate the similarities and differences between call, apply, bind and a variety of practical application demonstrations

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.