Home  >  Article  >  Web Front-end  >  Introduction and demonstration results of the difference between caller and callee_Basic knowledge

Introduction and demonstration results of the difference between caller and callee_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:40:34879browse

caller

caller returns a reference to a function that calls the current function.

Be careful when using this attribute:

1 This attribute is only useful when the function is executing
2 If in a javascript program, the function is called from the top level, null is returned

functionName.caller: functionName is currently being executed function.

Copy code The code is as follows:

var a = function() {
alert( a.caller);
}
var b = function() {
a();
}
b();

In the above code , b calls a, then a.caller returns a reference to b, the result is as follows:

If a is called directly (that is, a is called in any function, that is, a top-level call), return null:

Copy code The code is as follows:

var a = function() {
alert(a.caller);
}
var b = function() {
a( );
}
//b();
a();

Output result:

callee

callee puts back a reference to the executing function itself, which is an attribute of arguments

Be careful when using callee:

1 This attribute is only valid when the function is executed
2 It has a length attribute, which can be used to obtain the number of formal parameters, so it can be used to compare whether the number of formal parameters and actual parameters are consistent, that is, comparison Whether arguments.length is equal to arguments.callee.length
3 It can be used to recurse anonymous functions.

Copy code The code is as follows:

var a = function() {
alert( arguments.callee);
}
var b = function() {
a();
}
b();

a in b is called, but it returns a reference to a itself, and the result is as follows:

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