Home  >  Article  >  Web Front-end  >  Examples of using JavaScript anonymous functions and delegates_Basic knowledge

Examples of using JavaScript anonymous functions and delegates_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:41:251319browse
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
  <!-- C#匿名函数--> 
  <title></title> 
  <script type="text/javascript"> 
    var f1 = function (x, y) { //【1】 定义一个匿名函数,用变量f1来指向它(f1相当于一个委托,这个时候f1就可以当做一个函数来用了)  
      return x + y; 
    } 
 
    //调用这个匿名函数  
    alert(f1(5, 6)); //输出11  
    //【2】 还可声明匿名函数立即使用  
    alert(function (a, b) { return a + b } (10, 2)); //直接声明一个匿名函数function (a, b) { return a + b },然后直接使用function (a, b) { return a + b } (10, 2)。连指向匿名函数function (a, b) { return a + b }的变量f1都不用了。这里输出12  
 
    //【3】 没有参数的匿名函数  
    var f2 = function () { alert("你好") }; 
    f2(); //这里输出“你好”  
 
    var f3 = function () { return 5 }; 
    alert( f3() + 5);//输出10 
  </script> 
</head> 
<body> 
 
</body> 
</html>
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