IIFE(Immediate invoke function expression)sometime , we hope to define and invoke a function immediately .for this case , can have several ways to do .(function(){ /* code */ }()); // recommanded
(function(){ /* code */ })(); // But this one works just as well
// Because the point of the parens or coercing operators is to disambiguate// between function expressions and function declarations, they can be// omitted when the parser already expects an expression (but please see the// "important note" below).var i = function(){ return 10; }();
true && function(){ /* code */ }();
0, function(){ /* code */ }();
// If you don't care about the return value, or the possibility of making// your code slightly harder to read, you can save a byte by just prefixing// the function with a unary operator.!function(){ /* code */ }();
~function(){ /* code */ }();
-function(){ /* code */ }();
+function(){ /* code */ }();
new function(){ /* code */ }
new function(){ /* code */ }() // Only need parens if passing arguments
now introducing the module- pattern in JS :// Create an anonymous function expression that gets invoked immediately,// and assign its *return value* to a variable. This approach "cuts out the// middleman" of the named `makeWhatever` function reference.//// As explained in the above "important note," even though parens are not// required around this function expression, they should still be used as a// matter of convention to help clarify that the variable is being set to// the function's *result* and not the function itself.var counter = (function(){
var i = 0;
return {
get: function(){
return i;
},
set: function( val ){
i = val;
},
increment: function() {
return ++i;
}
};
}());
// `counter` is an object with properties, which in this case happen to be// methods.counter.get(); // 0
counter.set( 3 );
counter.increment(); // 4
counter.increment(); // 5
counter.i; // undefined (`i` is not a property of the returned object)
i; // ReferenceError: i is not defined (it only exists inside the closure)
The Module Pattern approach is not only incredibly powerful, but incredibly simple. With very little code, you can effectively namespace related methods and properties, organizing entire modules of
code in a way that both minimizes global scope pollution and creates privacy.reference : http://benalman.com/news/2010/11/immediately-invoked-function-expression/
分享到:
相关推荐
该插件可以将ES模块的输出转换为IIFE。 安装 npm install -D rollup-plugin-iife 用法 import iife from "rollup-plugin-iife" ; export default { input : [ "entry.js" , "entry2.js" ] , output : { dir : ...
babel-plugin-typescript-iife-enum目的TypeScript转换为IIFE中的包装器枚举。目的目前,TypeScript将从enum Test { Key = 1} 到var Test ;( function ( Test ) { Test [ Test [ "Key" ] = 1 ] = "Key" ;} ) ( Test ...
因果报应包装的预处理程序Karma预处理程序将文件内容包装在具有完整源映射支持的IIFE中安装npm install --save karma-wrap-iife-preprocessor用法学分执照国际学习中心
发现原来还有一种叫法是: 立即调用函数表达式 (IIFE,Immediately-Invoked Function Expression) 立即调用函数表达式 相比于自执行匿名函数,立即调用函数表达式的意思来得更明确一些。 自执行函数有下面的这一些...
定义一个IIFE: Instantly-Invoked Function Expression 定义scope chain 定义closure 介绍 本实验描述了一些与JavaScript函数有关的更高级的概念。 如果您对某个概念不满意,请务必花点时间尝试一下或继续阅读。 ...
基本功能回顾学习目标使用函数声明定义函数定义hoisting 定义function expression 定义anonymous function 使用函数表达式定义函数定义一个IIFE: Instantly-Invoked Function Expression 定义scope chain 定义...
基本功能回顾学习目标使用函数声明定义函数定义hoisting 定义function expression 定义anonymous function 使用函数表达式定义函数定义一个IIFE: Instantly-Invoked Function Expression 定义scope chain 定义...
定义一个IIFE: Instantly-Invoked Function Expression 定义scope chain 定义closure 介绍 本实验描述了一些与JavaScript函数有关的更高级的概念。 如果您对某个概念不满意,请务必花点时间尝试一下或继续阅读。 ...
基本功能回顾学习目标使用函数声明定义函数定义hoisting 定义function expression 定义anonymous function 使用函数表达式定义函数定义一个IIFE: Instantly-Invoked Function Expression 定义scope chain 定义...
前端开源库-gulp-iifeGulpIIfe,将JavaScript代码包装在立即调用的函数表达式中。
口香糖一个Gulp插件,用于将JavaScript代码包装在立即调用的函数表达式(IIFE)中。安装$ npm install --save-dev gulp-iife用法var gulp = require ( "gulp" ) ;var iife = require ( "gulp-iife" ) ;gulp . task ...
PokeScript关于通过Vanilla JavaScript创建的Pokemon应用程序,并使用CSS进行样式设置,这是CareerFoundry的Full-Stack Web开发课程的一部分。 这个简单的应用程序从外部API加载数据,在单独的按钮上显示每个对象,...
IIFE (Immediately Invoked Function Expression): 避免变数污染到global scope Array: .push() , .splice() , .sort() , .filter() , .map() , .join() , .forEach() this & .bind() & Arrow Function JSON: ....
本文主要介绍了javascript立即执行函数表达式IIFE的相关知识。具有很好的参考价值,下面跟着小编一起来看下吧
script src =" https://cdn.jsdelivr.net/npm/basekits@1/dist/basekits.iife.js " crossorigin type =" text/javascript " > </ script > < script src =" ...
最近在学习javascript的函数,函数是javascript的一等对象,想要学好javascript,就必须深刻理解函数。... 2.2:立即调用的函数表达式(IIFE, Immediately invoked function expression) 1.函数参
主要介绍了JavaScript函数IIFE使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
davidBowie-IIFE:封装囊封IIFE
3) [临时] 捆绑从项目的根目录输入rollup ./main.js --format iife --file ./pkg/bundle.js 。 注意:在实现wasm-pack 之前,没有可用的选项来。 在此期间,必须使用打包程序,例如 。 4) [可选] 试运行从项目的根...
前端开源库-cjs2iifecjs2ife,commonjs模块到iffe代码