Keywords: arguments, callee, caller
arguments: represents the parameters passed into the function
callee: Statement representing functions and function bodies
caller: represents the function that calls the function
arguments
This object represents the function being executed and the parameters of the function that calls it.
caller
Returns a reference to the function that called the current function.
functionName.caller
The functionName object is the name of the function being executed.
illustrate
For functions, the caller attribute is only defined when the function is executed. If the function is called from the top level, then caller contains null. If the caller attribute is used in a string context, the result is the same as functionName.toString, that is, the decompiled text of the function is displayed.
callee
Returns the Function object being executed, which is the body of the specified Function object.
[function.]arguments.callee
The optional function parameter is the name of the Function object currently being executed.
illustrate
The initial value of the callee attribute is the Function object being executed.
The callee attribute is a member of the arguments object. It represents a reference to the function object itself, which 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 related 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.
Copy the code code as follows:
<script type='text/javascript'>
function test(x,y,z)
{
alert("Actual parameter length:"+arguments.length);
alert("Formal parameter length:"+arguments.callee.length);
alert("Formal parameter length:"+test.length);
alert(arguments[ 0 ])
alert(test[ 0 ]) // undefined has no such usage
}
//test(1,2,3);
test(1,2,3,4);
/*
* arguments are not an array (Array class)
*/
Array.prototype.selfvalue = 1;
function testAguments() {
alert( " arguments.selfvalue= " + arguments.selfvalue);
}
alert("Array.sefvalue="+new Array().selfvalue);
testAguments();
/**/ /*
* Demonstrates the caller attribute of the function.
* Description: (current function).caller: returns a reference to the function that called the current function
*/
function callerDemo() {
if (callerDemo.caller) {
var a = callerDemo.caller.arguments[ 0 ];
alert(a);
} else {
alert( " this is a top function " );
}
}
function handleCaller() {
callerDemo();
}
callerDemo();
handleCaller("Parameter 1", "Parameter 2");
/**/ /*
* Demonstrates the callee attribute of the function.
* Description: arguments.callee: The initial value is the Function object being executed, used for anonymous functions
*/
function calleeDemo() {
alert(arguments.callee);
}
calleeDemo();
(function(arg0,arg1){alert("The number of shapes is: "+arguments.callee.length)})();
/**/ /*
* Demonstrate the usage of apply and call functions
* Note: The function is to bind the function to another object to run. The only difference between the two is the way to define parameters:
* apply(thisArg,argArray);
* call(thisArg[,arg1,arg2…] ]);
* That is, the this pointer inside all functions will be assigned thisArg
*/
functionObjectA() {
alert( "Execute ObjectA() " );
alert(arguments[ 0 ]);
this .hit = function (msg) {alert(msg)}
this .info = "I am from ObjectA"
}
functionObjectB() {
alert( "Execute ObjectB() " );
// Call the ObjectA() method, and all this in the ObjectA constructor will be replaced by this in ObjectB.
ObjectA.apply( this ,arguments); // ObjectA.call(this);
alert(this .info);
}
ObjectB('Parameter 0');
var value = "global variable";
function Obj() {
this .value = "Object!";
}
function Fun1() {
alert( this .value);
}
Fun1();
Fun1.apply(window);
Fun1.apply(new Obj());
</script>