Javascript call, callee, calle, apply

Source: Internet
Author: User
For the call method, see apply to: Function object requires Version 5.5 to call one method of an object and replace the current object with another object. The call ([thisObj [, arg1 [, arg2 [, [,. argN]) parameter is optional. Will be used as the object of the current object. Arg1, a... SyntaxHighl

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 or arguments.
There is callee and caller. This is different from the call usage. Let's talk about it next time.
Caller
Returns a reference to the function that calls the current function.
FunctionName. caller
The functionName object is the name of the executed function.
Description
For a function, the caller attribute is defined only when the function is executed. If the function is called by the top layer, caller contains null. If the caller attribute is used in the string context, the result is the same as functionName. toString, that is, the decompilation Text of the function is displayed.
The following example illustrates the usage of caller attributes:

// Caller demo {
Function callerDemo (){
If (callerDemo. caller ){
Var a = callerDemo. caller. toString ();
Alert ();
} Else {
Alert ("this is a top function ");
}
}
Function handleCaller (){
CallerDemo ();
}
Callee

Returns the Function object being executed, that is, the body of the specified Function object.
[Function.] arguments. callee
The optional function parameter is the name of the currently executed Function object.
Description
The initial value of the callee attribute is the Function object being executed.
The callee attribute is a member of the arguments object. It indicates a reference to the function object itself, which facilitates anonymity.
Recursion of a function or encapsulation of a function. For example, the following example recursively calculates the sum of natural numbers from 1 to n. This attribute
It is available only when the related function is being executed. Note that callee has the length attribute, which is sometimes
It is better for verification. Arguments. length is the length of the real parameter, and arguments. callee. length is
The length of the parameter to determine whether the length of the parameter is consistent with that of the actual parameter.
Example
Typical recursive functions: When called: alert (sum (100 ));
The function contains a reference to sum itself. The function name is only a variable name. Calling sum inside the function is equivalent to calling
A global variable cannot reflect the call itself. Using callee is a good method.
// Callee can print itself
Function calleeDemo (){
Alert (arguments. callee );
}
// Used to verify Parameters
Function calleeLengthDemo (arg1, arg2 ){
If (arguments. length = arguments. callee. length ){
Window. alert ("verify that the length of the form parameter and real parameter is correct! ");
Return;
} Else {
Alert ("real parameter length:" + arguments. length );
Alert ("parameter length:" + arguments. callee. length );
}
}
// Recursive Calculation
Var sum = function (n ){
If (n <= 0)
Return 1;
Else
Return n + arguments. callee (n-1)
}

Var sum = function (n ){
If (1 = n) return 1;
Else return n + sum (n-1 );

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.