A function is an event-driven or reusable block of code that executes when it is invoked.
A function is an object that has its own properties and methods. First of all, through the console output function attribute method to visually look at:
Function internal properties include only two special objects: arguments and this.
Function properties include: Length and prototype
Functional methods (not inherited) include: Apply () and call ()
Inherited function Methods: Bind (), toString (), tolocalestring (), valueof ()
The others are not ripe at the moment, and are later added
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 arguments for the passed function, and arguments's main purpose 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. The following is a very classical factorial function.
function factorial (num) {
if (num <= 1) {return
1;
} else{return
num * factorial (num-1);
}
The definition of a factorial function typically uses a recursive algorithm, 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 to the factorial name, and the Arguments.callee can be used to eliminate this tight coupling phenomenon (such as the change of function name).
function factorial (num) {
if (num<=1) {return
1;
} else{return
num * Arguments.callee (NUM-1);
}
}
The function body of the factorial () function is overridden, and no further reference is factorial to the functional name. In this way, even if the function name is changed, the recursive call is guaranteed to complete normally. For example:
var truefactorial = factorial; Change the pointer (save position) of the original function body
factorial = function () {//factorial point to the new function returning 0 return
0;
}
Alert (Truefactorial (5));
Alert (factorial (5));//0
If the Arguments.callee is not used, then truefactorial (5) also returns 0;
This property
2. Method of function
Each function contains two methods that are not inherited: Apply () and call (). The purpose of both methods is to call a function in a particular domain (see the wood here to understand); its real strength is in being able to expand the scope in which the function is run
About JavaScript function Internal properties and function methods of knowledge, small series to introduce so many people, I hope to help you!