What are arguments, caller, and callee in javascript? What is the role of javascript? This article will give a basic introduction to this. I hope you will like it.
What is arguments?
When calling a function, arguments creates a similar array but not an array object, and stores the parameters actually passed to the function, it is not limited to the parameter list of function declaration.
What does Nima mean?
Write a demo. See the code below.
Arguments
Script function obj () {// use instanceof to determine arguments console. log ('arguments instanceof Array? '+ (Arguments instanceof Array); console. log ('arguments instanceof Object? '+ (Arguments instanceof Object); console. log (arguments);} obj (); script
Run the code and use the chrome debugger to obtain
I use instanceof to judge arguments. According to the print result, arguments is an object.
Then expand the output arguments. We can see that it contains many attributes, as well as callee.
Next, we modify the above Code and pass parameters to the obj function when calling the obj function, but the obj function does not have parameters.
For specific code, see
Arguments
Script function obj () {console. log ('arguments instanceof Array? '+ (Arguments instanceof Array); console. log ('arguments instanceof Object? '+ (Arguments instanceof Object); console. log (arguments);} // pass the parameter obj ('monkey', 'love', 24) to obj; script
Use the chrome debugger to obtain
As you can see, arguments contains three parameters we pass to it: "monkey", "love", and 24.
So why does arguments actually store the parameters passed to the function, rather than the declared parameters.
What is callee?
Callee is a member of the arguments object. Its value is "Function object being executed ".
What does it mean?
Let's write a demo and check the output.
The code and result are shown in the following figure.
Callee
Script function obj () {// use callee console. log (arguments. callee);} obj (); script
As shown in the preceding figure, arguments. callee is a function pointing to the arguments object. Here it is obj.
What is caller?
Caller is an attribute of a function object that stores the function that calls the current function.
Note: It is called. It not only contains closures. If there is no parent function, it is null.
It's still the same. Let's keep writing a demo.
The Code is as follows:
Caller
Script // child is a function in parent and executes the child function parent () {function child () {// in parent. The parent function of child is the parent console. log (child. caller) ;}child () ;}// parent1 is not called by someone else. function parent1 () {// parent1 has no parent function console. log (parent1.caller);} // parent2 calls child2 function parent2 () {child2 ();} function child2 () {console. log (child2.caller);}/* execute the parent nested child function parent1. No nested function parent2 calls child2. child2 is not a function nested in parent2 */parent (); parent1 (); parent2 (); script
Open the chrome debugger.
Can I understand caller in combination with code and understanding?