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
(function(global,factory){"use strict";if(typeofmodule==="object"&&typeofmodule.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){thrownewError("jQuery requires a window with a document");}returnfactory(w);};}else{factory(global);}// Pass this if window is not defined yet}(typeofwindow!=="undefined" ? window : this,function(window,noGlobal){//........//amdif(typeofdefine==="function"&&define.amd){define("jquery",[],function(){returnjQuery;});}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;}returnjQuery;};// Expose jQuery and $ identifiers, even in AMD// and CommonJS for browser emulators (#13566)if(!noGlobal){window.jQuery=window.$=jQuery;}//return objreturnjQuery;}))
JavaScript 模块化编程
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框架使用的是一个自执行函数包裹
moment和jquery的实现方式几乎一样 只是把amd cmd的检测放到了一起做判断,jquery是在工厂函数中进行amd的判断 命名冲突检测 全局暴露等操作;
The text was updated successfully, but these errors were encountered: