三种自执行函数写法
-
在要执行的函数后面添加一个括号然后再将它们用括号包起来
// 这种方法也适用于箭头函数 (function hello(){alert('hello')}()); (()=>{alert('hello')}());
-
将要执行的函数用括号包起来,然后在包起来的函数后面添加一个括号
// 这种方法不能使用箭头函数写法 (function hello(){alert('hello');} ());
-
在要执行的函数前面添加一元运算符(!||void),然后在要执行的函数后面添加括号
// 这种方法同样不能使用箭头函数写法 !function hello(){alert('hello')}(); void function hello(){alert('hello')}();