关于匿名函数自执行

赋值式(将匿名函数赋给变量)表达式定义法    声明式
            var fn_01 = function(){
                alert("匿名函数");
            }
            fn_01(); //如果想调用fn_01函数,写该函数的名称,然后写小括号就可以了
            
 将一个匿名函数用小括号括起来,后面加一个小括号,表示匿名函数的自执行(可以防止变量污染)
            (function(){
                alert("匿名函数");
            })();
            
带参数的匿名函数自执行
            (function(a,b){   //形参:定义函数时接收数据的参数
                alert(a + b);
            })(1,2);
            
有返回值的匿名函数的自执行
            var res = (function(a,b){
                return a > b ? a : b;
            })(3,4);
            alert(res);
            
将一个匿名函数赋值给一个表达式,在该匿名函数后加一个小括号,表示匿名函数自执行
            var fn_02 = function(){
                alert("匿名函数");
            }();

            alert( fn_02 );
            var fn_03 = function(){
                return 23;
            }();
            alert( fn_03 );     //fn_02接收了匿名函数的返回值
            
拓展小知识:
            ~ function(){console.log("hello world1");}();
            ! function(){console.log("hello world2");}();
            + function(){console.log("hello world3");}();
            - function(){console.log("hello world4");}();


            delete function(){console.log("hello world5");}();
            void function(){console.log("hello world6");}();
            
            var result = 1 + function(){return 1}();
            alert(result);     //2
            
            (function(){console.log("hello world7");})();
            (function(){console.log("hello world8");}());
           

转载于:https://www.cnblogs.com/lisaShare/p/9000017.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值