函数声明和函数表达式的区别和使用
区别:函数声明:
function fun(){
}
函数表达式:
var abc=function fun(){
console.log("123");
}
//以下为具体案例
abc()
fun()
console.log(abc.name)
console.log(fun.name)
console.log(abc.length)
console.log(fun.length)
//输出的结果分别为123和fun is not defined ,abc此时是一个函数,当它变成了一个表达式后,就会放弃原自己
的名字
在函数前面加上+,-,%,可以让函数声明变成函数表达式
函数表达式可以直接被执行符号执行
+function(){
console.log("hello")
}()
//函数声明了以后直接自己执行,用函数的返回值,适合做初始化的工作;避免写全局变量,所有的内容全部放在立即执行函数中