"The best JavaScript design is the implementation of its functions." "--the essence of JavaScript language
The function contains a set of statements, which are the basic module units of JS that specify the behavior of the object. In general, the so-called programming is the ability to break down a set of requirements into a set of functions and data structures.
1. Function functions
Code repeatability use
Modular programming
2. Syntax:
You need to define before using the function to use
The function definition has three parts: function name, argument list, function body
Format:
1 function function name ([parameter 1, Parameter 2, ...]) {23 function execution part; 4 5 return an expression; 6 7 }
For example:
1 <script>2 function display (first,second) {3 alert ( first+second); 4 }5 var i=10; 6 var j=20; 7 Display (i,j); 8 </script>
In the above example, First,second is a formal parameter and i,j is an argument.
During the execution of a function, the parameters that are worth changing do not affect the arguments. (Passed by value)
However, the object data type is passed by reference (passed by address), and other data types are passed by value.
For example, in the following example, the result of the operation is "AAA"
1 <script>2 function display (obj) {3 obj.name= ' aaa ' ; 4 }5 varnew Object (); 6 P.name = ' BBB '; 7 Display (p); 8 alert (p.name); 9 </script>
3. Questions about the return value of a function
The function returns immediately when it encounters a return, and the following code does not execute.
4. About anonymous functions
In JS each variable, including the function is an object, all occupy the memory address. A function is also an object that occupies a memory address. Memory includes, heap memory, stack memory, static zone, and code snippet. In general, variable names are saved to the stack memory, and the data for the variable objects is stored in the heap memory. Because the heap is larger than the stack, the stack is faster than the heap. The function is saved to the in-memory code snippet. The reference to the data in the code snippet is pointed to by a name in the stack memory.
1<script>2 //A variable can hold data, or it can save an address3 vari=10;4 alert (i);5 6 functiondisplay () {7Alert (' 123 ');8 }9 TenI=display;//The variable points to the first address of the function . OneI ();//Eject 123 A</script>
In the above example, function display () {} is actually adding a variable called display under the Window object, which points to the first address of the function. What i=dispaly means is that we let I point to the first address of the function under the Window object.
Use of anonymous functions
1<script>2 //the early stages of anonymous functions3 varI=functiondisplay () {4Alert (' 123 ');5 }6 I ();7 8 //use of anonymous functions9 varj=function(){TenAlert (' 123 '); One } A j (); -</script>
5. Self-invocation of anonymous functions
Format: (function () {}) ();
function () {}: equivalent to returning the first address
(function () {}): Consider this part as a whole
(function () {}) (): Equivalent to finding this address and performing
1<script>2 //self-invoking anonymous functions3(function(){4Alert (' 111 ');5 })();6 7 //self-invoking anonymous functions with parameters8(function(para) {9 alert (para);Ten}) (222); One</script>
The benefit of self-invoking anonymous functions is to avoid duplicate names, since invoking an anonymous function is performed only once at run time and is typically used for initialization.
6. Global variables and local variables
1 <script>2 i=10; // global variable 3 // global variable 4 function display () { var k=30; // 6 p=40; // global variable 7 8 </script>
The definition within a function is local, otherwise it is global. Local variables work only at the local scope. If a variable within a function has no VAR declaration, it will directly affect the global.
Mechanism: In JS, if a variable does not have a VAR declaration, it will automatically go to the previous scope to find the declaration statement of the variable, if found, use, if not found, continue to look up, until the global scope. If the declaration statement of this variable is still not in the global scope, it is automatically declared in the global scopes, which is the scope chain in JS.
Local access globally through the scope chain, the global access to the local through the closure.
Use of 7.arguments
Inside a function, you can use the Arguments property, which represents the formal parameter list of a function, as an array.
When defining a function, its number of arguments must be consistent with the number of formal parameters, and sometimes, when we define a function, the number of formal parameters cannot be fixed, and this is the time to use arguments.
1<script>2 functionShowName () {3 //no formal parameters are defined, then all the participants are automatically stored in the arguments attribute array4 for(vari=0;i<arguments.length;i++){5document.write (Arguments[i] + ' <br> ');6 }7 }8ShowName (' Zhangsan ', ' Lisi ', ' Wangwu ');9</script>
No formal parameters are defined, then all the participants are automatically stored in the arguments attribute array.
JS function-function, syntax, return value, anonymous function, self-invocation anonymous function, global variable and local variable, arguments use