A function is a reusable block of code that is driven by an event or executed when it is called. This article focuses on introducing functions to you about blind spot summary of basic knowledge of js.
1. The arguments object in the function
Each function has an argument inside, which can return all parameters accepted by the function.
Note: arguments receive actual parameters
The following is a sum function written using the arguments feature:
function sumOnSteroids(){ var I, res = 0; var number_of_params = arguments.length; for(I = 0; I < number_of_params; i++){ res += arguments[i]; } return res; }2. About the scope of variables
In javascript, a specific block-level scope cannot be defined for a variable, but the function domain it belongs to can be defined.
Global variables in javascript refer to variables declared outside of all functions
Local variables defined inside a function do not exist outside the function
If a variable is declared inside a function without using a var statement, the variable will be defaulted to a global variable (although it can be accessed outside the function). This variable will not exist before the function is called, and will be created for the first time and given to the global scope after calling the function.
3. Self-modulating function
Self-keying functions are functions that can be called by themselves after definition. The basic structure is as follows:
{ function(name){ alert('Hello' + name + '!'); } }('Martin')The following () can be passed parameter, and the declared value in it will be automatically passed to the function parameter.
Using the above self-tuning anonymous function will not produce any global variables
4. Functions that can be rewritten by themselves
The characteristic of this function is that it can rewrite the function by itself after execution. Here is an example:
function a(){ alert("A"); a = function(){ alert("B") }; }When a() is called for the first time, alert("A"); when a() is called for the second time, alert("B") is executed. Because when the function is executed for the first time, it redefined the global variable a
The above is the relevant knowledge of the functions of the basic Javascript knowledge blind spot summary introduced to you by the editor. I hope it will help you!