IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.
/**
* 優(yōu)點:可以傳參
*/
(function (){
// sth
})();
(function IIFE() {
// sth
})();
/**
* 優(yōu)點:節(jié)省代碼量
*/
(function () {
// sth
});
(function IIFE() {
// sth
});
/**
* 非主流寫法懂从,容易引起別人誤解挠乳,但是最簡潔
*/
!function () {
// sth
}();
!function IIFE() {
// sth
}();
/**
* 后調用寫法尾菇,很多人認為這種寫法更容易理解業(yè)務結構
*/
(function (def) {
def(window);
})(function (global) {
// sth
});
(function IIFE(def) {
def(window);
})(function def(global) {
// sth
});