Home  >  Article  >  Web Front-end  >  Introduction and examples of caller and callee attributes in js_Basic knowledge

Introduction and examples of caller and callee attributes in js_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:45:351778browse

1. caller

Returns a reference to the function that called the current function.

functionName.caller

The functionName object is the name of the executed function.

Description

For functions, the caller attribute is only defined when the function is executed. If the function is called from the top level of a Javascript program, then caller contains null .

The following example illustrates the usage of the caller attribute:

Copy code The code is as follows:

function callerDemo() {
if ( arguments. caller) {
var a= callerDemo.caller.toString();
alert(a);
} else {
alert("this is a top function");
}
}
function handleCaller() {
callerDemo();
}
handleCaller();
function calleeDemo() {
alert(arguments.callee);
}
calleeDemo();


2. callee

Returns the Function object being executed, which is the body of the specified Function object.

[function.]arguments.callee
Optional function parameter is the name of the Function object currently being executed.

Description

The initial value of the callee attribute is the Function object being executed.

The callee attribute is a member of the arguments object, which represents a reference to the function object itself. This is beneficial to the recursion of anonymous functions or to ensure the encapsulation of functions. For example, the following example recursively calculates the sum of natural numbers from 1 to n. This property is only available when the relevant function is executing. It should also be noted that callee has a length attribute, which is sometimes better for verification. arguments.length is the actual parameter length, and arguments.callee.length is the formal parameter length. From this, you can determine whether the formal parameter length is consistent with the actual parameter length when calling.

Example

Copy code The code is as follows:

//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 formal and actual parameter lengths are correct!");
          return; ;
alert("Formal parameter length: " arguments.callee.length);
}
}
//Recursive calculation
var sum = function(n){
if ( n < = 0)                                                                                                                                                                                                                                      .




Copy code

The code is as follows:

var sum = function(n){ if (1==n) return 1;else return n sum (n-1); When calling: alert(sum(100));
The function contains a reference to sum itself. The function name is just a variable name. Calling sum inside the function is equivalent to calling a global variable. It cannot well reflect that it is calling itself. At this time Using callee would be a better method.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn