JavaScript匿名函數與委託,javascript匿名函數

來源:互聯網
上載者:User

JavaScript匿名函數與委託,javascript匿名函數

<1>

<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匿名函數

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

其實函數都可以傳遞參數的,就看你在函數體內有沒有做相應的處理:

匿名函數有形參的時候,就可以傳遞相應的實參:
(function(a, b)
{
alert(a + " and " + b);
}
)("a", "b"); // 返回"a and b"

匿名函數沒有形參的時候,可以用arguments來處理:
(function()
{
var str = ""
for(i = 0; i < arguments.length; i ++ )
{
str += arguments[i] + " , ";
}
alert(str);
}
)("a", "b", "c", "d"); //返回"a , b , c , d , "
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.