1、封闭函数
1、常规写法
代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>封闭函数</title>
<script type="text/javascript">
function myalert() {
alert("Hello World!!");
}
myalert();
</script>
</head>
<body>
</body>
</html>
2、封闭函数
代码如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>封闭函数</title>
<script type="text/javascript">
// 1、常规写法
/*
function myalert() {
alert("Hello World!!");
}
myalert();
*/
// 封闭函数的写法
(function myalert() {
alert("Hello World!!");
})();
// 封闭函数
(function () {
var iNum01=24; // 封闭函数内部的局部变量
myalert02();
function myalert02() {
alert('hello haha');
}
})();
</script>
</head>
<body>
</body>
</html>