Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript模块化 #23

Open
yanyue404 opened this issue Apr 24, 2018 · 0 comments
Open

Javascript模块化 #23

yanyue404 opened this issue Apr 24, 2018 · 0 comments

Comments

@yanyue404
Copy link
Owner

yanyue404 commented Apr 24, 2018

Javascript模块化

前言

探究 Javascript 模块化进程,感叹发展迅速!

IIFE

(Immediately-Invoked Function Expression) 自执行函数

var myGradesCalculate = (function () {
    
  // Keep this variable private inside this closure scope
  var myGrades = [93, 95, 88, 0, 55, 91];
  
  var average = function() {
    var total = myGrades.reduce(function(accumulator, item) {
      return accumulator + item;
      }, 0);
      
    return'Your average grade is ' + total / myGrades.length + '.';
  };

  var failing = function() {
    var failingGrades = myGrades.filter(function(item) {
        return item < 70;
      });

    return 'You failed ' + failingGrades.length + ' times.';
  };

  // Explicitly reveal public pointers to the private functions 
  // that we want to reveal publicly

  return {
    average: average,
    failing: failing
  }
})();

myGradesCalculate.failing(); // 'You failed 2 times.' 
myGradesCalculate.average(); // 'Your average grade is 70.33333333333333.'

CommonJS

Node.js 的 module.exports导出 与 require 的导入,采用同步模式。

AMD

(Asynchronous Module Definition),代表require.js框架,充分利用浏览器的并发异步加载能力

UMD

(Universal Module Definition),前后端跨平台的解决方案(支持AMD与CommonJS模块方式),。

// if the module has no dependencies, the above pattern can be simplified to
(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define([], factory);
    } else if (typeof module === '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();
  }
}(typeof self !== '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 {};
}));

ES6 Modules

关键字就是importexport,作为 JavaScript 官方标准,日渐成为了开发者的主流选择。通过 Babel 等转化工具能帮我们巴 ES6 的模块机制 转化为 CommonJS 兼容。

参考文章

@yanyue404 yanyue404 changed the title js模块化 Javascript模块化 May 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant