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

Examples of using JavaScript anonymous functions and delegates_Basic knowledge

WBOY
Release: 2016-05-16 16:41:25
Original
1318 people have browsed it
<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>
Copy after login
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!