Function types of Javascript: 1. Constructor, use the new keyword to define the function to be called, and return a new object by default; 2. Anonymous function, a function without an actual name, will not cause pollution to global variables; 3. Closure function , a function with permission to access variables in another function scope; 4. Dynamic function, the parameter list and function body are functions dynamically specified through strings.
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
1. Constructor
The constructor in Javascript refers to a function defined and called using the new keyword, which returns a new object by default.
2.Anonymous function
Anonymous functions in Javascript refer to functions without actual names. Using anonymous functions will not cause pollution of global variables.
3. Closure function
A closure function in Javascript refers to a function that has permission to access variables in the scope of another function. Closure functions are often created inside a function.
4.Dynamic functions
A dynamic function in Javascript refers to a function whose parameter list and function body are dynamically specified through a string.
Examples are as follows:
<script> function show(){ alert("The first..."); } function show(str){ alert("The second"); } function show(a,b){ alert("The third . . . "); alert(a+":"+b); } </script> </head> <!-- Functions with variable parameters: They are all functions with variable parameters in js. 1 Although the function is declared as two parameters when it is defined, any number can be passed in when it is called --> <!-- 2 In each function, there is a default array arguments, which stores the parameters passed during this call. All actual parameters entered --> <body> <!-- 1. Demonstration of variable parameters: --> <script> show();//When called, the previous ones will be flushed //undefined: undefined show(111);//When called, the previous ones will be flushed///11:undefined show("a","b");//After called, the previous ones will be flushed//a:b show(1,2,3,4);//1:2 </script>To sum up the above examples, there is no overloading of functions in JS. It must be wildcarded for all. Although the function declares several variables when it is defined, any number can be passed in when it is called. In each function, there is a default array arguments, which stores all the actual parameters passed in during this call.
anonymous function
<!DOCTYPE html> <html> <head> </head> <body> <!-- Demonstrate JavaScript anonymous function--> <script type="text/javascript"> var res =function(a,b){/ /Note that it is lowercase func return a+b; };</span> alert("sum="+res(1,2));//sum=3 alert("res="+res("abc"," def"));//res=abcdef </script> </body> </html>dynamic function
Introduction: Use the built-in object Function in Js to construct a function. The first parameter in the construction method is the "formal parameter" and the second parameter is the "function body".
<span style="font-size:18px;"><!DOCTYPE html> <html> <head> <title>DTfunc.html</title> <meta http-equiv="keywords" content="keyword1,keyword2, keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!-- <link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <!-- Use the built-in object Function in Js to construct a function. The first parameter in the constructor is the "formal parameter" and the second parameter is the "function body". --> <!-- The idea is similar to class reflection in Java. We usually don't use it when writing functions, but once we write the key points, the function of the entire program will become very flexible --> <script> var res=new Function("x,y","var sum=0;sum= x+y;return sum;") var sum=res(12,34);//46 var sum=res("abc","bss");//abcbss alert("sum="+sum); < /script> </body> </html></span>