1.目的
JavaScript封閉函數
2.示例代碼
圖片及源碼的github鏈接
016.封閉函數
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>016.封閉函數</title>
<script type="text/javascript">
// 一個封閉函數(匿名函數),創建+執行
(function () {
alert('Hello World!');
})();
// 普通全局變量和函數
var iNum1 = 12;
function fnA() {
alert('This is Outter A');
}
fnA(); // 運行
// 封閉函數
(function () {
// 內部定義變量
var iNum1 = 24;
// 封閉函數內部定義函數
function fnA() {
alert('This is inner A');
}
fnA(); // 運行
})();
alert("Num: " + iNum1); // 12
</script>
</head>
<body>
</body>
</html>