A function is an object that has its own properties and methods. First, the output of the console under the function attribute method to visually look at:
- The function's internal properties include only two special objects: arguments and this.
- Function properties include: Length and prototype
- function methods (non-inheritance) include:apply () and call ()
- Inherited function Methods: Bind (), toString (), tolocalestring (), ValueOf ()
- The others are not ripe at the moment, and the back is replenished
1. Function Internal Properties
Inside the function, there are two special objects, arguments and this.
Arguments Property
Arguments is a class array object that contains all the parameters of the passed-in function, and the main purpose of arguments is to save the function arguments, but this object has a callee property, which is a pointer, To the function that owns the arguments object , here is the very classic factorial function.
function factorial (num){ if1){ return1; else{ return num * factorial(num-1); }}
A recursive algorithm is commonly used to define factorial functions, as shown in the code above, which is fine when there is a function name and the function name does not change. However, the execution of this function is tightly coupled with the name factorial, and in order to eliminate this close coupling phenomenon ( such as change of function name ), Arguments.callee can be used.
function factorial(num){ if(num<=1){ return1; else{ returnarguments.callee(num-1); }}
the factorial () function is overridden by the function body, and no further reference to the name factorial. This way, even if you change the function name, you can ensure that recursive calls are completed normally. For example:
var trueFactorial = factorial; //改变原函数体的指针(保存位置)function (){//factorial 指向返回0的新函数 return0;}alert(trueFactorial(5)); //120alert(factorial(5)); //0
If you do not use Arguments.callee, then truefactorial (5) also returns 0;
This property is 2. Methods of the function
Each function contains two non-inherited methods: Apply () and call (). The purpose of both methods is to invoke the function in a particular domain (see the wood here); its real strength is the ability to extend the scope of functions on which they run
Keep writing tomorrow.
JavaScript functions Internal properties and Function methods