Summary of Call,apply,bind method in JS

Source: Internet
Author: User

Why What's call,apply,bind doing? Why do you want to learn this?

This is typically used to specify the environment for this, which is usually the problem before it is learned.

var a = {    " small Mazar ",    fn:function () {        Console.log (  This . user);}    } var b =undefined

We want to print the user in object A but print out what's going on with the undefined? If we directly execute A.fn () is possible.

var a = {    " small Mazar ",    fn:function () {        Console.log (  This . User);     // Little Mazar

It is possible to print here because this is pointing to function A, so why doesn't it point to a? If we need to understand the point of this issue.

Although this method can achieve our goal, but sometimes we have to save this object to another variable, then you can use the following methods.

1. Call ()
var a = {    " small Mazar ",    fn:function () {        Console.log (  this. user); // Little Mazar      var

With the call method, the first parameter is added to which environment to add B to, in a nutshell, this will point to that object.

The call method can add multiple parameters in addition to the first argument, as follows:

var a = {    " small Mazar ",    fn:function (NUM1, num2) {        Console.log (this. user);   // Little Mazar        Console.log (NUM1 + num2);  // 3     }}var b =12);

2. Apply ()

The Apply method is somewhat similar to the call method, and it can also change the point of this

var a = {    " small Mazar ",    fn:function () {        Console.log ( this. user);  // Little Mazar     }}var b = a.fn;b.apply (a);

The same applies can have multiple parameters, but the difference is that the second parameter must be an array, as follows:

var a = {    " small Mazar ",    fn:function (NUM1, num2) {        Console.log (this. user);  // Little Mazar        Console.log (NUM1 + num2);  //  One     }}var b = a.fn;b.apply (A, [1]);

Note If the first argument to call and apply is NULL, then this refers to the Window object

var a = {    " small Mazar ",    fn:function () {        Console.log (  This // Window {external:object, Chrome:object, Document:document, A:object, speechsynthesis:speechsynthesis ...}     }}var b = a.fn;b.apply (null);

3. Bind ()

The Bind method is somewhat different from the call and apply methods, but they can be used to change the point of this anyway.

Let's talk about their differences first.

var a = {    " small Mazar ",    fn:function () {        Console.log (  This . user);}    } var b = A.fn;b.bind (a); Console.log (B.bind (a));   //  ? () {console.log (this.user);}

We found that the code was not printed, yes, that's the difference between bind and call, the Apply method, and the Bind method actually returns a modified function.

So let's run the function C and see if we can print out the user in object A.

var a = {    " small Mazar ",    fn:function () {        Console.log ( this. user);  // Little Mazar     }}var b = A.fn; var c = b.bind (a); C ();

OK, the same bind can have multiple parameters, and the parameters can be executed again when added, but note that the parameters are in the order of the formal parameter.

var a = {    " small Mazar ",    fn:function (e,d,f) {        Console.log ( this //  small Mazar        //  1 2    }}var B = A.fn; var c = B.bind (A,ten); C (1,2);

Summary: Call and apply are changes in the context of this and immediately execute this function, the bind method can let the corresponding function when it is called when the calling, and the parameters can be added at the time of execution, this is their difference, according to their actual situation to choose to use.

Summary of Call,apply,bind method in JS

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.