The difference between the call (), apply (), and bind () methods in JS

Source: Internet
Author: User

Call (Thisobj,param1,param2 ...) Method: Invokes the method of an object to replace the current object with another object.

An example is given below:

function Add (A, b) {

return a+b;

}

function Sub (A, b) {

return a-B;

}

Add.call (sub,3,2)

When you execute the above code, the result of the output is 5. Executes Add.call (sub,3,2) ==add (3,2), which is equivalent to replacing a sub with add; Each function is also an object, which is an instance of function.

Write another example that changes the function execution context:

function Animal () {

This.name= ' Animal ';

This.sayname=function () {

Console.log (this.name);

}

}

Function Dog () {

This.name= ' dog ';

}

var animal=new animal ();

var dog=new dog ();

Animal.sayName.call (dog);}

The above code is equivalent to the animal object in the Sayname method placed on the dog object execution, its execution environment changed to dog, so sayname method of this point to the current dog object, so this.name for dog.

Changes the current point of this, which points to the current Thisobj object.

B\ Implementing inheritance

Instance: function Animal (name) {

This.name=name;

This.sayword=function () {

Console.log (this.name);

}}

function Dog (name) {

Animal.call (This,name)

}

var dog=new dog (' I am a big dog ');

Dog.sayname ();

The output is I am a big dog; but there is no Sayname method in the dog constructor, why is it called here, in fact Animal.call (This,name) is equivalent to replacing the This object with a animal object, Then there are the properties and methods of the animal object in the constructor. The resulting instance object, dog, can use these properties and methods, but one thing to note is that the current this is a pointer to the dog object that called the method.

C\ Implementing multiple Inheritance

function Animal (name) {

This.name=name;

This.sayword=function () {

Console.log (this.name);

}}

function Human () {

This.sayname=function () {

Console.log (' Hello Human ')

}

}

function Dog (name) {

Animal.call (This,name)

Human.call (This)

}

var dog=new dog (' I am a big dog ');

Dog.sayname ();

Dog.sayword ();

Multiple inheritance can be implemented by using call () multiple times, inheriting related properties and methods.

These are some of the applications of the call () method, and the Apply () method is similar to call (), except that the second parameter accepted by the Apply () method is an array, and call is a parameter list form. The rest is no different. Usage is similar to the call () method.

The following emphasis is on the bind () method, which is a new method of JS, as with the call and apply method, the same as the call method, the only difference is that the calling () and the Apply () method is called directly and return the result, and bind () method returns a function that needs to be called further. Examples are as follows:

var obj1={

Name: ' Chen ',

Age:18,

Say:function (name,age) {

Console.log (this.name+ "" +this.age)

}

}

var obj2={

Name: ' Ze ',

Age:12

}

Obj1.say.call (OBJ2);

Obj1.say.apply (OBJ2);

Obj1.say.bind (OBJ2);

The result of the above execution is

Ze 12
Ze 12
function (name,age) {

Console.log (this.name+ "" +this.age)
}

You can see that the bind () method returns a function that needs to be called further to return the corresponding result.

The difference between the call (), apply (), and bind () methods 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.