JavaScript Tutorial: Learning the Call function as an instance

Source: Internet
Author: User
Tags object implement inheritance

The call () in JavaScript is a fascinating approach, but it's also a confusing way to look at the official explanation first:

Call method

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.

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.

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. More interestingly, 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 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.

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 does not belong to 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 a arguments.



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.