Describes the Internal Attributes and Function Method examples of JavaScript Functions, and details about javascript
A function is an event-driven or reusable code block that is executed when it is called.
A function is an object and has its own attributes and methods. First, let's take a look at the function attribute method output in the console:
The function's internal attributes only include two special objects: arguments and this.
Function attributes include length and prototype.
Function Methods (non-inheritance) include: apply () and call ()
Inherited Function Methods: bind (), toString (), toLocaleString (), valueOf ()
Others are not familiar at present. I will try again later
1. Internal Function Attributes
Within the function, there are two special objects: arguments and this.
Arguments attributes
Arguments is a class array object that contains all the parameters of the input function. The main purpose of arguments is to save the function parameters, but this object has a callee attribute, which is a pointer, point to the function that owns the arguments object. Below is a very classic factorial function.
function factorial (num){if(num <= 1){return 1;} else{return num * factorial(num-1); }}
Recursive Algorithms are generally used to define a factorial function. As shown in the code above, this definition is okay if a function name is available and the function name will not change. However, the execution of this function is closely coupled with the function name factorial. To eliminate this close coupling (function name changes, etc.), you can use arguments. callee.
function factorial(num){if(num<=1){return 1;} else{return num * arguments.callee(num-1);}}
The rewritten factorial () function does not reference the function name factorial any more. In this way, even if the function name is changed, recursive calling can be completed normally. For example:
Var trueFactorial = factorial; // change the pointer (save position) of the original function body factorial = function () {// factorial points to the new function return 0 ;} alert (trueFactorial (5); // 120 alert (factorial (5); // 0
If arguments. callee is not used, trueFactorial (5) returns 0;
This property
2. Function Methods
Each function contains two non-inherited methods: apply () and call (). The purpose of these two methods is to call a function in a specific domain (I can see it here). Its real strength lies in its ability to expand the scope on which the function depends.
For more information about the internal attributes and methods of JavaScript Functions, I hope to help you!
Articles you may be interested in:
- How to implement method overloading in js? And function parameters
- Conflict between jquery and js Functions
- Multiple methods to execute a js function after the jsp page is fully loaded
- A comprehensive javascript URL parsing function and segmented URL Parsing Method
- Detailed explanation of four call methods for JavaScript Functions
- Introduction to Internal Attributes of javascript Functions