JavaScript Call Method Use Description _ Basics

Source: Internet
Author: User
Tags inheritance
Let's take a look at the official explanation:
Call method
Please see
Apply to: Function Object
Requirements
Version 5.5
Invokes one of the object's methods to replace the current object with another object.
Call ([thisobj[,arg1[, arg2[, [,. argn]]]]
Parameters
Thisobj
Options available. The object that will be used as the current object.
Arg1, Arg2, argn.
Options available. A sequence of method parameters is passed.
Description
The call method can be used to invoke a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by Thisobj.
If the thisobj parameter is not supplied, the Global object is used as a thisobj.
At first glance, it's easy to look at people and make some simple explanations.
Obj1.method1.call (Obj2,argument1,argument2)
As above, call's role 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 ");
Well, it's interesting that you can get a object to execute the B-object method, something that the Java programmer doesn't want to think about. And more interestingly, you can 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");
Yes, that's it, that's 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);
}

It's easy to use two call to achieve multiple inheritance.
Of course, JS inheritance There are other methods, such as the use of prototype chain, this is not part of the scope of this article, but here to explain the use of call
Call and, of course, apply, these two methods are basically a meaning
The difference is that the second argument to call can be any type, and the second argument to apply must be an array, or it can be a arguments
There are callee,caller, this and call usage is different, put it next time, hehe.

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.