Home > Web Front-end > JS Tutorial > body text

Detailed explanation of commonly used function types in JavaScript_javascript skills

WBOY
Release: 2016-05-16 15:31:20
Original
1484 people have browsed it

The java code in the web page needs to be written in JavaScript, and some functions are indispensable. Let’s introduce the commonly used function types in JavaScript.
1. Variable function

    <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> 
Copy after login


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.

2. 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> 
Copy after login

3. 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> 
<!--  利用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> 
Copy after login

The three commonly used function types in JavaScript shared above are only briefly introduced. If you want to learn more about them, you can continue reading the related articles shared below.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template