123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <!--
- 1.理解
- *全称:Immediately-Invoked Function Expression,又叫匿名函数自调用
- 2.作用
- *隐藏实现
- *不会污染外部(全局)命名空间
- *用它来编写js模块
- -->
- <script>
- // (function () { //匿名函数自调用
- // console.log('---------');
- // })();
- // (function () { //匿名函数自调用
- // var a = 3;
- // console.log(a+3);//输出6
- // })();
- // var a = 4;
- // console.log(a);//输出4
- (function(){
- var a = 1;
- function test(){
- console.log(++a);
- };
- function test2(){
- console.log('test2()');
- };
- window.$ = function(){ //向外暴露一个全局函数
- return{
- test:test
- };
- };
- })();
- $().test(); //1.$是一个函数 2.$执行后返回的是一个对象
- </script>
- </body>
- </html>
|