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模块化编程简介 #2

Open
gnipbao opened this issue Apr 14, 2017 · 0 comments
Open

JavaScript模块化编程简介 #2

gnipbao opened this issue Apr 14, 2017 · 0 comments

Comments

@gnipbao
Copy link
Owner

gnipbao commented Apr 14, 2017

JavaScript 模块化编程

201206221140432
JavaScript本身不是一种模块化语言,设计者在创造JavaScript之初应该也没有想到这么一个脚本语言的作用领域会越来越大。以前一个页面的JS代码再多也不会多到哪儿去,而现在随着越来越多的JavaScript库和框架的出现,Single-page App的流行以及Node.js的迅猛发展,如果我们还不对自己的JS代码进行一些模块化的组织的话,开发过程会越来越困难,运行性能也会越来越低。因此,了解JS模块化编程是非常重要的。

为什么要使用模块化开发

可以看看requirejs官方的解释说的非常详细
why web modules

CommonJS

AMD

CMD

UMD

UMD是AMD和CommonJS的糅合,AMD 浏览器第一的原则发展 异步加载模块。CommonJS 模块以服务器第一原则发展,选择同步加载,它的模块无需包装(unwrapped modules)。这迫使人们又想出另一个更通用的模式UMD (Universal Module Definition)。希望解决跨平台的解决方案。

下面看下一些经典库的封装基本都是基于UMD方式的

  • jquery
(function(global, factory) {

	"use strict";

	if (typeof module === "object" && typeof module.exports === "object") {

		// For CommonJS and CommonJS-like environments where a proper `window`
		// is present, execute the factory and get jQuery.
		// For environments that do not have a `window` with a `document`
		// (such as Node.js), expose a factory as module.exports.
		// This accentuates the need for the creation of a real `window`.
		// e.g. var jQuery = require("jquery")(window);
		// See ticket #14549 for more info.
		module.exports = global.document ?
			factory(global, true) :
			function(w) {
				if (!w.document) {
					throw new Error("jQuery requires a window with a document");
				}
				return factory(w);
			};
	} else {
		factory(global);
	}

	// Pass this if window is not defined yet
}(typeof window !== "undefined" ? window : this, function(window, noGlobal) {

	//........

	//amd
	if (typeof define === "function" && define.amd) {
		define("jquery", [], function() {
			return jQuery;
		});
	}

	var
	// Map over jQuery in case of overwrite
		_jQuery = window.jQuery,

		// Map over the $ in case of overwrite
		_$ = window.$;

	jQuery.noConflict = function(deep) {
		if (window.$ === jQuery) {
			window.$ = _$;
		}

		if (deep && window.jQuery === jQuery) {
			window.jQuery = _jQuery;
		}

		return jQuery;
	};

	// Expose jQuery and $ identifiers, even in AMD
	// and CommonJS for browser emulators (#13566)
	if (!noGlobal) {
		window.jQuery = window.$ = jQuery;
	}
	//return obj
	return jQuery;
}))

jquery框架使用的是一个自执行函数包裹

(function(xx,oo){
//...
}(xx,oo))
  • moment.js
;(function (global, factory) {
   //三元表达式进行commonjs amd的检测
    typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
    typeof define === 'function' && define.amd ? define(factory) :
    global.moment = factory()
}(this, (function () { 
'use strict';
//工厂函数
})));

moment和jquery的实现方式几乎一样 只是把amd cmd的检测放到了一起做判断,jquery是在工厂函数中进行amd的判断 命名冲突检测 全局暴露等操作;

@gnipbao gnipbao changed the title javascript模块化编程 javaScript模块化编程 Dec 28, 2019
@gnipbao gnipbao changed the title javaScript模块化编程 JavaScript模块化编程简介 Dec 28, 2019
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