JS call Method

Source: Internet
Author: User
Call Method
See
Applied to: Function objects
Requirements
Release 5.5
Call a method of one object to replace the current object with another object.

Call ([thisobj [, arg1 [, arg2 [, [,. argn])
Parameters
Thisobj
Optional. Will be used as the object of the current object.
Arg1, arg2, and argn
Optional. The method parameter sequence will be passed.
Description
The call method can be used to call 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 provided, the global object is used as thisobj.

Bytes -------------------------------------------------------------------------------------------
At first glance, it is easy to confuse people and give some simple instructions.
Obj1.method1. Call (obj2, argument1, argument2)
As shown above, the call function is to put the obj1 Method on obj2 for use, and the argument1. these are passed as parameters.

A specific example
Function add (A, B)
{
Alert (A + B );
}
Function Sub (A, B)
{
Alert (a-B );
}

Add. Call (sub, 3, 1 );

In this example, we use add to replace sub and Add. call (sub, 3, 1) = add (3, 1), so the running result is: Alert (4); // Note: Functions in JS are actually objects, the function name is a reference to the function object.

Let's look at a slightly more complex 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: Call means to put the C1 Method on C2 for execution. Originally, C2 does not have the shownam () method. Now, it is to put the shownam () method of C1 to C2 for execution, so this. the name should be class2, And the execution result is: Alert ("class2 ");

How about it? I think it's interesting. It's hard for Java programmers to let object a execute the method of object B. In addition, call can be used to implement inheritance.

Function class1 ()
{
This. showtxt = function (txt)
{
Alert (txt );
}
}

Function class2 ()
{
Class1.call (this );
}

VaR C2 = new class2 ();

C2.showtxt ("cc ");

In this way, class2 inherits class1. class1.call (this) means that the class1 object is used to replace this object. Then, does class2 have all the attributes and methods of class1, the C2 object can directly call the class1 Method and Its Attributes. The execution result is: Alert ("cc ");

That's right. This is how JavaScript simulates inheritance in object-oriented systems, and implements 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 multi-inheritance with two calls.
Of course, there are other methods of JS inheritance, such as using prototype chain. This is not part of the scope of this article, but only describes the call usage here.
When we talk about call and apply, these two methods basically mean one thing.
The difference is that the second parameter of call can be of any type, and the second parameter of apply must be an array Reprinted from: http://www.cnblogs.com/sweting/archive/2009/12/21/1629204.htmlcall Method
See
Applied to: Function objects
Requirements
Release 5.5
Call a method of one object to replace the current object with another object.

Call ([thisobj [, arg1 [, arg2 [, [,. argn])
Parameters
Thisobj
Optional. Will be used as the object of the current object.
Arg1, arg2, and argn
Optional. The method parameter sequence will be passed.
Description
The call method can be used to call 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 provided, the global object is used as thisobj.

Bytes -------------------------------------------------------------------------------------------
At first glance, it is easy to confuse people and give some simple instructions.
Obj1.method1. Call (obj2, argument1, argument2)
As shown above, the call function is to put the obj1 Method on obj2 for use, and the argument1. these are passed as parameters.

A specific example
Function add (A, B)
{
Alert (A + B );
}
Function Sub (A, B)
{
Alert (a-B );
}

Add. Call (sub, 3, 1 );

In this example, we use add to replace sub and Add. call (sub, 3, 1) = add (3, 1), so the running result is: Alert (4); // Note: Functions in JS are actually objects, the function name is a reference to the function object.

Let's look at a slightly more complex 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: Call means to put the C1 Method on C2 for execution. Originally, C2 does not have the shownam () method. Now, it is to put the shownam () method of C1 to C2 for execution, so this. the name should be class2, And the execution result is: Alert ("class2 ");

How about it? I think it's interesting. It's hard for Java programmers to let object a execute the method of object B. In addition, call can be used to implement inheritance.

Function class1 ()
{
This. showtxt = function (txt)
{
Alert (txt );
}
}

Function class2 ()
{
Class1.call (this );
}

VaR C2 = new class2 ();

C2.showtxt ("cc ");

In this way, class2 inherits class1. class1.call (this) means that the class1 object is used to replace this object. Then, does class2 have all the attributes and methods of class1, the C2 object can directly call the class1 Method and Its Attributes. The execution result is: Alert ("cc ");

That's right. This is how JavaScript simulates inheritance in object-oriented systems, and implements 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 multi-inheritance with two calls.
Of course, there are other methods of JS inheritance, such as using prototype chain. This is not part of the scope of this article, but only describes the call usage here.
When we talk about call and apply, these two methods basically mean one thing.
The difference is that the second parameter of call can be of any type, and the second parameter of apply must be an array.

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.