Call functions in Javascript

Source: Internet
Author: User

Javascript Medium Call Function usage instructions

Keywords: Web/JavaScriptIn JavaScriptCall() Is a wonderful method, but it is also a confusing method. Let's take a look at the official explanation:

CallMethod
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
CallA method can be used to call a method instead of another object.CallThe object context of a function can be changed 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,CallThe 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:JSThe function in is actually an object, and 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,CallIt 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. We can let object a execute the method of object B. This is JavaProgramMembers do not dare to think about it. You can also useCallTo 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, if class2 does not have all the attributes and methods of class1, then the C2 object can directly call the class1 method and 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 use twoCallMultiple inheritance is implemented.
Of course,JSThere are other methods to inherit from, such as using prototype chain. This does not fall into the scope of this article, but it is described hereCallUsage
SaidCallAnd, of course, apply. These two methods basically mean
The difference is thatCallThe second parameter of can be of any type, and the second parameter of apply must be an array or arguments
Callee, caller, andCallThe usage is different. Let's talk about it next time.

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.