JS Call () method

Source: Internet
Author: User

Call method See
Apply to: Function Object
Requirements
Version 5.5
Invokes one method of an object, replacing the current object with another object.

Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])
Parameters
Thisobj
Options are available. The object that will be used as the current object.
Arg1, Arg2,, ArgN
Options are available. The method parameter sequence will be passed.
Description
The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by Thisobj.

If the Thisobj parameter is not provided, then the Global object is used as the thisobj.

-------------------------------------------------------------------------------------------
At first glance, it's easy to get people to look at a fan and do some simple instructions first.
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 passed as arguments.

To give a specific example
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, and the function name is a reference to a function object.


Look at a slightly more complicated example.

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 Shownam () method, is now C1 the Shownam () method put to C2 to execute, so this.name should be class2, the result of execution is: alert (" Class2 ");

How, think interesting, can let a object to execute the method of B object, this is the Java programmer dare not think of. And more interesting, you can use call to implement inheritance.

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

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

var C2 = new Class2 ();

C2.showtxt ("CC");

So Class2 inherit Class1, Class1.call (this) means to use the Class1 object instead of the This object, then Class2 all the properties and methods Class1, C2 object can directly call Class1 method to and attributes, the result of execution is: alert ("CC");

Right, that's it, that's how JavaScript simulates inheritance in object-oriented, and can also implement multiple inheritance.

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 implement multiple inheritance with two call
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, just here to explain the use of call
Said call, and of course, apply, these two methods are basically a meaning
The difference is that the second argument of call can be any type, and the second argument of apply must be an array "reprint" http://www.cnblogs.com/sweting/archive/2009/12/21/1629204.html

JS Call () method

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.