IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.
/**
* 优点:可以传参
*/
(function (){
// sth
})();
(function IIFE() {
// sth
})();
/**
* 优点:节省代码量
*/
(function () {
// sth
});
(function IIFE() {
// sth
});
/**
* 非主流写法,容易引起别人误解,但是最简洁
*/
!function () {
// sth
}();
!function IIFE() {
// sth
}();
/**
* 后调用写法,很多人认为这种写法更容易理解业务结构
*/
(function (def) {
def(window);
})(function (global) {
// sth
});
(function IIFE(def) {
def(window);
})(function def(global) {
// sth
});
网友评论