在瀏覽器中,受網絡和瀏覽器渲染的制約,不能採用同步加載,只能採用異步加載。因而 AMD 規範應運而生javascript
AMD(Asynchronous Module Definition),意思就是"異步模塊定義"。它採用異步方式加載模塊,制定了定義模塊的規則,這樣模塊和模塊的依賴能夠被異步加載,不影響它後面語句的運行。全部依賴這個模塊的語句,都定義在一個回調函數中,等到加載完成以後,這個回調函數纔會運行。這和瀏覽器的異步加載模塊的環境恰好適應(瀏覽器同步加載模塊會致使性能、可用性、調試和跨域訪問等問題)css
本規範只定義了一個函數 "define",它是全局變量 define(id?, dependencies?, factory),參數分別是模塊名,依賴,工廠方法html
<script type=」text/javascript」 defer async=」true」 src=」./require.js」></script> <script type=」text/javascript」 defer async=」true」 src=」js/init.js」></script>
//require.config 主要是用來對要加載文件的目錄進行自定義 require.config({ baseUrl: 'js', paths: { "jquery": "../lib/jquery", "undersocre": "../lib/underscore", } }) require(['jquery', 'underscore'], function ($, _) { $(window).resize(function () { var color = ["rgba(", Math.floor(Math.random() * 255), ",", Math.floor(Math.random() * 255), ",", Math.floor(Math.random() * 255), ")"]; $(".background").css({ position: "fixed", top: "0px", bottom: "0px", left: "0px", right: "0px", background: color.join("") }); }) });
第一個參數是一個數組,值是依賴的模塊。回調事件會在全部依賴模塊加載完畢後纔會執行java
該規範解決的瀏覽器環境下如何編寫代碼實現模塊化,該規範定義可模塊的一些遵循的特徵,來支持能共用的模塊jquery
define(factory)定義模塊git
define(function(require, exports, module) { // do something });
每一個模塊中都有個名叫"exports"的自由變量,這是一個模塊能夠在模塊執行時添加模塊 API 的對象。github
懶加載,在 require 時候纔會加載模塊api
這是 seajs 對象上綁定的屬性和方法跨域
color.js數組
define("color", function(require, exports, module) { var \$ = require("jquery"); var createColor = function() { return ["rgba(", Math.floor(Math.random() * 255), ",", Math.floor(Math.random() * 255), ",", Math.floor(Math.random() * 255), ")"]; }; module.exports = { changeBg: function() { \$("#bg").css({ position: "fixed", top: "0px", bottom: "0px", left: "0px", right: "0px", background: createColor().join("") }); } }; });
使用非函數的工廠包裝模塊 text.js
define({ text: "我是初始化程序", text2: "我要開始執行了" });
init.js
define("init", function(require, exports, module) { var color = require("../src/color"); var initText = require("../src/text"); var \$ = require("jquery"); module.exports = { start: function() { console.log(initText.text + "," + initText.text2); $(function() { $("#change").click(function() { color.changeBg(); }); }); } }; });
sea.js.html
... <body id="bg"> <button id="change">點我我變色</button> </body> <script src="./lib/sea.js"></script> <script> seajs.config({ alias: { underscore: "underscore.js", init: "./src/init.js", color: "./src/color.js" } }); seajs.use(["underscore", "init"], function(u, init) { init.start(); }); </script> ...
目錄結構
通常控制檯報錯 xxx is not a function
一些庫不支持模塊引入或者只支持 amd 規範的引入方式,不支持 cmd。全部須要對庫進行一些改造
//Underscore.js 1.9.1 if (typeof define === "function" && define.amd && define.amd.jQuery) { define("underscore", [], function() { return \_; }); } //更改以下 if (typeof define === "function" && (define.amd || define.cmd)) { define("underscore", [], function() { return _; }); } //或者整個 define 的判斷不要了 if (typeof define === "function") { define("underscore", [], function() { return _; }); }
是一種思想,就是一種兼容 commonjs,AMD,CMD 的兼容寫法,define.amd / define.cmd / module 等判斷當前支持什麼方式,都不行就掛載到 window 全局對象上面去
(function (root, factory) { if (typeof define === 'function' && (define.amd || define.cmd)) { //AMD,CMD define(['b'], function(b){ return (root.returnExportsGlobal = factory(b)) }); } else if (typeof module === 'object' && module.exports) { //Node, CommonJS之類的 module.exports = factory(require('b')); } else { //公開暴露給全局對象 root.returnExports = factory(root.b); } }(this, function (b) { return {}; }));