JavaScript匿名函數與委託使用樣本,javascript樣本

來源:互聯網
上載者:User

JavaScript匿名函數與委託使用樣本,javascript樣本

<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>

Javascript匿名函數定義及舉例?

就是沒有函數名的函數,一經定義,無法被其它程式段使用(因為沒有函數名)

舉例:

var a;
a.b = function(){alert('hello');}

這時你要調用那個函數,就必須用a.b的方式訪問,而不像普通的函數,

在c#2.0也有匿名函數,
 
javascript匿名函數

這個是對象裡面的函數,
Blog.blogSorter(b1,b2);//直接這麼調用就可以了.
對象是Blog,
blogSorter是屬於Blog對象裡面的函數.
明白了沒.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.