You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varmyGradesCalculate=(function(){// Keep this variable private inside this closure scopevarmyGrades=[93,95,88,0,55,91];varaverage=function(){vartotal=myGrades.reduce(function(accumulator,item){returnaccumulator+item;},0);return'Your average grade is '+total/myGrades.length+'.';};varfailing=function(){varfailingGrades=myGrades.filter(function(item){returnitem<70;});return'You failed '+failingGrades.length+' times.';};// Explicitly reveal public pointers to the private functions // that we want to reveal publiclyreturn{average: average,failing: failing}})();myGradesCalculate.failing();// 'You failed 2 times.' myGradesCalculate.average();// 'Your average grade is 70.33333333333333.'
// if the module has no dependencies, the above pattern can be simplified to(function(root,factory){if(typeofdefine==='function'&&define.amd){// AMD. Register as an anonymous module.define([],factory);}elseif(typeofmodule==='object'&&module.exports){// Node. Does not work with strict CommonJS, but// only CommonJS-like environments that support module.exports,// like Node.module.exports=factory();}else{// Browser globals (root is window)root.returnExports=factory();}}(typeofself!=='undefined' ? self : this,function(){// Just return a value to define the module export.// This example returns an object, but the module// can return a function as the exported value.return{};}));
Javascript模块化
前言
探究 Javascript 模块化进程,感叹发展迅速!
IIFE
(Immediately-Invoked Function Expression) 自执行函数
CommonJS
Node.js 的
module.exports
导出 与require
的导入,采用同步模式。AMD
(Asynchronous Module Definition),代表
require.js
框架,充分利用浏览器的并发异步加载能力UMD
(Universal Module Definition),前后端跨平台的解决方案(支持AMD与CommonJS模块方式),。
ES6 Modules
关键字就是
import
与export
,作为 JavaScript 官方标准,日渐成为了开发者的主流选择。通过 Babel 等转化工具能帮我们巴 ES6 的模块机制 转化为CommonJS
兼容。参考文章
The text was updated successfully, but these errors were encountered: