JavaScript Learning Notes (ix) Call and Apply Methods _ basics

Source: Internet
Author: User
Tags inheritance
Call and Apply methods
The call method can change the context this pointer, and similar methods apply, primarily to keep the current this instance pointer consistent, or to change the this pointer in special cases, when each method of the JS object calls each other.
Obj1.method1.call (Obj2,argument1,argument2)
As above, the role of call is to put the Obj1 method on the Obj2 to use, the back of the argument1 ... These are introduced as parameters.
Give a concrete example.
Copy Code code as follows:

function Add (A, b) {
Alert (a + B);
}
function Sub (A, b) {
alert (a-b);
}
Add.call (Sub, 3, 1);

The meaning of this example is to replace sub,add.call (sub,3,1) = Add (3,1) with add, so the result is: alert (4); Note: The function in JS is actually an object, the function name is a reference to a function object.
Look at a slightly more complicated example.
Copy Code code as follows:

function Class1 () {
THIS.name = "Class1";

This.shownam = function () {
alert (this.name);
}
}

function Class2 () {
THIS.name = "Class2";
}

var C1 = new Class1 ();
var C2 = new Class2 ();

C1.showNam.call (C2);

Note that call means to put the C1 method on the C2, the original C2 is not the Shownam () method, now is the C1 Shownam () method to carry out the C2, so this.name should be class2, the result of execution is: alert (" Class2 ");
You can also use call to implement inheritance
Copy Code code as follows:

function Class1 () {
This.showtxt = function (TXT) {
alert (TXT);
}
}

function Class2 () {
Class1.call (this);
}

var C2 = new Class2 ();

C2.showtxt ("CC");

So Class2 inherited Class1, Class1.call (this) means to use Class1 object instead of this object, then Class2 all the properties and methods of Class1, C2 object can directly call the Class1 method to and attributes, the result is: alert ("CC");
This is how javaScript simulates object-oriented inheritance, and it can also implement multiple inheritance.
Copy Code code as follows:

function Class10 () {
This.showsub = function (A, b) {
alert (a-b);
}
}

function Class11 () {
This.showadd = function (A, b) {
Alert (a + B);
}
}

function Class2 () {
Class10.call (this);
Class11.call (this);
}

1.call method
Invokes one of the object's methods to replace the current object with another object.
Call ([thisobj[,arg1[, arg2[, [,. argn]]]]
Parameters
Thisobj can be selected. The object that will be used as the current object.
Arg1, ARG2, argn options available. A sequence of method parameters is passed.
2.apply method
Applies a method of an object, replacing the current object with another object.
Apply ([Thisobj[,argarray]])
Parameters
Thisobj can be selected. The object that will be used as the current object.
Argarray can be selected. An array of arguments to be passed to the function.

the difference between the two
The function of the two implementations is exactly the same, except that the parameters are passed in a different way, call is to separate the arguments by commas (,), and apply is to pass all the parameters into an array.
Related Article

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.