There are two special properties inside the function: arguments and this. arguments is an array object of class that contains all the parameters passed in.
But this object also has a property called callee, which is a pointer to a function that owns the arguments object.
Please see the classic factorial function example:
The code copy is as follows:
function Factorial(num) {
if (num <= 1) {
return 1;
} else {
return num * Factorial(num - 1);
}
}
function Factorial(num) {
if (num <= 1) {
return 1;
} else {
return num * arguments.callee(num - 1);
}
}
There is nothing wrong with using the first method, but the coupling is too high and not very good. After the function name is changed, the internal function name must also be changed.
The second method is low coupling, no matter how the function name is changed, it will not affect the function execution.
This refers to the environment object on which the function is executed, or it can also be said to be this value.
The code copy is as follows:
window.color = "red";
var o = {color: "blue"};
function saysColor() {
alert(this.color);
}
sayColor();//red
o.sayColor = sayColor;
o.sayColor();//blue
Caller attribute holds a reference to the function that calls the current function. If the current function is called in the global scope, its value is Null
The code copy is as follows:
function outer() {
innter();
}
function inner(){
//alert(innter.caller);//The coupling is too high
alert(arguments.callee.caller);
}
outer();
The above is all the content of the internal properties of javascript functions. I hope you can like it