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