the javascript apply, call and bind the difference

In JS, these three points are used to change the function of this object, what kind of difference they have it.
Before the difference is still said to sum up the three similarities:
1, are used to change the point of this object's function.
2, the first argument is to point to this object.
3, subsequent parameters can use parameter passing.
So where is the difference between them, and look at an example.

  1.                 var xw = {
  2.                         name: "Amy"
  3.                         gender : "男",
  4.                         age : 24,
  5.                         say : function() {
  6.                                 alert(this.name + " , " + this.gender + " ,今年" + this.age);                                
  7.                         }
  8.                 }
  9.                 where xh = {
  10.                         name: "Little Red"
  11.                         gender : "女",
  12.                         age : 18
  13.                 }
  14.                 xw.say();
Copy the code

Itself is nothing to say, the display is certainly Wang, male, 24 this year.
So How to say method to display data xw xh it.
For the call can be:

  1. xw.say.call(xh);
Copy the code

You can apply for this:

  1. xw.say.apply(xh);
Copy the code

And it needs to bind this:

  1. xw.say.bind(xh)();
Copy the code

If you write xw.say.bind (xh) will not have any immediate results, see the difference yet? call and apply all direct calls to the function, while still bind method returns a function, and therefore need to back () to be called can.
So call and apply what difference does it? We have examples of some redrafting.

  1.                 var xw = {
  2.                         name: "Amy"
  3.                         gender : "男",
  4.                         age : 24,
  5.                         say : function(school,grade) {
  6.                                 alert(this.name + " , " + this.gender + " ,今年" + this.age + " ,在" + school + "上" + grade);                                
  7.                         }
  8.                 }
  9.                 where xh = {
  10.                         name: "Little Red"
  11.                         gender : "女",
  12.                         age : 18
  13.                 }
Copy the code

The method can be seen say more than two parameters, we pass by reference parameters call / apply the.
For this call is

  1. xw.say.call (xh, "Experimental Primary School", "sixth grade");       
Copy the code

For apply is this

  1. xw.say.apply (xh, [ "Experimental Primary School", "sixth grade Zhengzhou psoriasis hospital "]);
Copy the code

See the difference yet, call back and say parametric method is one to one, and apply the second argument is an array, the array elements are and say one to one method, which is both the largest difference.
So how bind parameter passing it? It can be passed as parameters like call.

  1. xw.say.bind (xh, "Experimental Primary School", "sixth grade") ();
Copy the code

However, due to still bind a function to return, so we can then pass parameters when invoked.

  1. xw.say.bind (xh) ( "Experimental Primary School", "sixth grade");

This switched: https://www.cnblogs.com/cosiray/p/4512969.html

Guess you like

Origin www.cnblogs.com/ruilin/p/11613203.html