When we say an application is modular, we generally mean it's composed of a set of highly decoupled, distinct pieces of functionality stored in modules.
当我们说一个程序是模块化的,通常我们指它是由一组存放在模块中的高度解耦的独立功能片段所组成。
http://addyosmani.com/writing-modular-js/
http://www.tuicool.com/articles/UZnq6j
可维护性
- 方便组合,分解
- 方便单个模块调试,升级
- 多人协作,互不干扰
可测试性
- 单元测试
性能损耗
The official JavaScript specification defines APIs for some objects that are useful for building browser-based applications. However, the spec does not define a standard library that is useful for building a broader range of applications.
The CommonJS API will fill that gap by defining APIs that handle many common application needs, ultimately providing a standard library as rich as those of Python, Ruby and Java.
// math.js
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
// increment.js
var add = require('math').add;
exports.increment = function(val) {
return add(val, 1);
};
// program.js
var inc = require('increment').increment;
var a = 1;
inc(a); // 2
module.id == "program";
The Asynchronous Module Definition (AMD) API specifies a mechanism for defining modules such that the module and its dependencies can be asynchronously loaded. This is particularly well suited for the browser environment where synchronous loading of modules incurs performance, usability, debugging, and cross-domain access problems.
It is unrelated to the technology company AMD and the processors it makes.
require
Sets up the module with ID of "alpha", that uses require, exports and the module with ID of "beta":
define("alpha", ["require", "exports", "beta"], function (require, exports, beta) {
exports.verb = function() {
return beta.verb();
//Or:
return require("beta").verb();
}
});
An anonymous module that returns an object literal:
define(["alpha"], function (alpha) {
return {
verb: function(){
return alpha.verb() + 2;
}
};
});
define(['require'], function (require) {
//the require in here is a local require.
});
define(function (require, exports, module) {
//the require in here is a local require.
});
define(function (require) {
var a = require('a');
});
define(function (require) {
require(['a', 'b'], function (a, b) {
//modules a and b are now available for use.
});
});
define(['text!../templates/start.html'], function (template) {
//do something with the template text string.
});
{
baseUrl: './foo/bar'
}
{
shim: {
'some/thing': {
deps: ['a', 'b'],
exports: 'some.thing',
init: function (a, b) {
return some.thing + 'another';
}
}
}
}
{
paths: {
"some": "some/v1.0"
}
}
RequireJS
curlJS
Dojo Loader
// name.js
YUI.add('name', function(Y) {
Y.namespace('mt').name = 'lyc';
});
// index.js
YUI({
modules: {
'name': './name.js'
}
}).use('name', function(Y) {
Y.log(Y.mt.name);
});
ES6实现了模块功能,试图解决JavaScript代码的依赖和部署上的问题,取代现有的CommonJS和AMD规范,成为浏览器和服务器通用的模块解决方案。
// profile.js
export var firstName = 'David';
export var lastName = 'Belle';
export var year = 1973;
import {firstName, lastName, year} from './profile';
function setHeader(element) {
element.textContent = firstName + ' ' + lastName;
}