Home  >  Article  >  Web Front-end  >  What are the function types of Javascript?

What are the function types of Javascript?

WBOY
WBOYOriginal
2022-06-29 16:30:464732browse

Javascript function types: 1. Constructor, use the new keyword to define the called function, 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 the scope of another function; 4. Dynamic function, the parameter list and function body are functions dynamically specified through strings.

What are the function types of Javascript?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

What are the function types of Javascript

1. Constructor function

The constructor in Javascript refers to a function that is called using the new keyword, and by default returns a new Object,

2. Anonymous function

Anonymous function in Javascript refers to a function without an actual name. 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 function

Dynamic function in Javascript refers to a function whose parameter list and function body are dynamically specified through a string.

The examples are as follows:

    <script> 
  
    function show(){ 
      alert("第一个。。。"); 
    } 
     
   
    function show(str){ 
    alert("第二个"); 
    } 
    function show(a,b){ 
      alert("第三个。。。"); 
      alert(a+":"+b); 
    } 
     </script> 
 </head> 
<!--  可变参数的函数: 在js中都是可变参数的函数 
<!-- 1 函数虽然定义时是声明成两个参数,但调用时却是可以传入任意个 --> 
<!-- 2 每个函数中,存在一个 默认的数组arguments ,里面存储着本次调用时传入的所有实参 --> 
 <body> 
<!-- 1, 可变参数的演示: --> 
 <script> 
     
    show();//当调用之后,会把前面的冲掉 //undefined:undefined 
    show(111);//当调用之后,会把前面的冲掉 // 11:undefined 
    show("a","b");//当调用之后,会把前面的冲掉//a:b 
    show(1,2,3,4);//1:2 
 </script>

Based on 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> 
<!--   演示JavaScript匿名函数 --> 
  <script type="text/javascript"> 
    var res =function(a,b){//注意是小写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, construct The first parameter in the 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> 
<!--  利用Js当中内置的对象Function来构造一个函数,构造方法中的第1个参数是“形参”,第2个参数是“函数体”。  --> 
<!-- 该思想类似于Java当中的类反射。我们平时写函数时通常不用,但关键的地方一写,整个程序的功能会变得很活 --> 
  <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>

[Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of What are the function types of Javascript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn