立即执行函数

一、立即执行函数 :此类函数没有声明,在一次执行过后立即释放。适合做初始化工作。


二、只有表达式才能被执行符号执行,执行后立即释放

  能被执行符号执行的表达式,自动忽略函数名
//+,-,!, ||,&&
+function test(){
    console.log(123);
}();//error: test is not defined

false|| function test(){
    console.log(1231)
}()//error: test is not defined

true && function test(){
    console.log(1231)
}()//error: test is not defined

var test=function(){
    console.log(1234);
}(); //test为undefined
var x=1;
if(function f(){}){ //f成为表达式
    x+=typeof f;//f未被定义,只有typeof 不会报错
}
console.log(x); //1undefined

 

posted @ 2018-08-10 15:22  yuesu  阅读(378)  评论(0编辑  收藏  举报