执行匿名函数,并且匿名函数中的变量不污染全局变量空间。
代码:
- <html>
- <head>
- <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
- </head>
- <body>
- <script type="text/javascript">
- (function(){
- var inside="inside";
- document.writeln("<p>函数0 无返回值,运行,未污染全局变量空间<br>");
- document.writeln("I am running in a function anonymous.<br>");
- document.writeln("var inside undefined :" + (typeof inside === 'undefined') + "<br>");
- document.writeln("var inside values: " + inside + "<br>");
- })();
- var res1 = (function () {
- var inside="inside";
- document.writeln("<p>函数1 有返回值,运行,未污染全局变量空间<br>");
- document.writeln("I am running in a function anonymous.<br>");
- document.writeln("var inside undefined :" + (typeof inside === 'undefined') + "<br>");
- document.writeln("var inside values: " + inside + "<br>");
- return 111;
- })();
- var res2 = function () {
- var inside="inside";
- document.writeln("<p>函数2 有返回值,运行,未污染全局变量空间<br>");
- document.writeln("I am running in a function anonymous.<br>");
- document.writeln("var inside undefined :" + (typeof inside === 'undefined') + "<br>");
- document.writeln("var inside values: " + inside + "<br>");
- return 222;
- }();
- var res3 = function foo() {
- var inside="inside";
- document.writeln("<p>函数3 有返回值,运行,未污染全局变量空间<br>");
- document.writeln("I am running in a naming function.<br>");
- document.writeln("var inside undefined :" + (typeof inside === 'undefined') + "<br>");
- document.writeln("var inside values: " + inside + "<br>");
- return 333;
- }();
- function bar() {
- var inside="inside";
- document.writeln("<p>函数4 有返回值,定义未运行,未污染全局变量空间<br>");
- document.writeln("I am running in a naming function.<br>");
- document.writeln("var inside undefined :" + (typeof inside === 'undefined') + "<br>");
- document.writeln("var inside values: " + inside + "<br>");
- return 444;
- };
- var res4 = bar(); // bar运行
- document.writeln("<p>全局,所有匿名函数未污染全局变量空间<br>");
- document.writeln("var inside undefined :" + (typeof inside === 'undefined'));
- document.writeln("<br>");
- document.writeln("function foo undefined :" + (typeof foo === 'undefined'));
- document.writeln("<br>");
- document.writeln("function bar undefined :" + (typeof bar === 'undefined'));
- document.writeln("<br>");
- document.writeln("匿名函数1返回值:" + res1);
- document.writeln("<br>");
- document.writeln("匿名函数2返回值:" + res2);
- document.writeln("<br>");
- document.writeln("命名函数foo定义并调用的返回值:" + res3);
- document.writeln("<br>");
- document.writeln("命名函数bar调用的返回值:" + res4);
- document.writeln("<br>");
- </script>
- </body>
- </html>
执行结果:
- 函数0 无返回值,运行,未污染全局变量空间
- I am running in a function anonymous.
- var inside undefined :false
- var inside values: inside
- 函数1 有返回值,运行,未污染全局变量空间
- I am running in a function anonymous.
- var inside undefined :false
- var inside values: inside
- 函数2 有返回值,运行,未污染全局变量空间
- I am running in a function anonymous.
- var inside undefined :false
- var inside values: inside
- 函数3 有返回值,运行,未污染全局变量空间
- I am running in a naming function.
- var inside undefined :false
- var inside values: inside
- 函数4 有返回值,定义未运行,未污染全局变量空间
- I am running in a naming function.
- var inside undefined :false
- var inside values: inside
- 全局,所有匿名函数未污染全局变量空间
- var inside undefined :true
- function foo undefined :true
- function bar undefined :false
- 匿名函数1返回值:111
- 匿名函数2返回值:222
- 命名函数foo定义并调用的返回值:333
- 命名函数bar调用的返回值:444