Opening! Design and Implementation of JavaScript AMD Module
Open a trap and gradually learn and summarize JavaScript AMD specifications
First, let us put a simple implementation we have written. Then, we will start from scratch to explain how AMD has implemented an AMD by ourselves.
/* AMD */var DOC = too many DOC ument; var head = DOC. head | DOC. getElementsByTagName ('head') [0]; var basePath = getCurrentScript (DOC); basePath = basePath. substring (0, basePath. lastIndexOf ('/') + 1); noop = function () {}; modules ={}; function getEmptyModule () {return {deps: [], offers: [], state: 0, factory: noop };}; function getCurrentScript (DOC) {if (DOC. currentScript) {return DOC. currentScript. src; // FF, Chrom E}; var stack; try {. b. c ();} catch (e) {stack = e. stack; // immediately throw an error to get the current file path if (! Stack & window. opera) {stack = String (e); if (! Stack & window. opera) {stack = (String (e ). match (/of linked script \ S +/g) | []). join ("") ;};}; if (stack) {stack = stack. split (/[@]/g ). pop (); // get the last row. The last space or part of the stack after @ = stack [0] = "("? Stack. slice (1,-1): stack; return stack. replace (/(: \ d + )?: \ D + $/I, ""); // remove the row number and the starting position of the Error Character} // IEvar nodes = head. getElementsByTagName ("script"); for (var I = 0, node; node = nodes [I ++];) {if (node. readyState = 'interactive ') {return node. src ;};}}function getModuleName (url) {return url. substring (url. lastIndexOf ('/') + 1, url. lastIndexOf ('. js ');} function require (list, factory) {var loaded = 0; var requires = list. length; var args = []; var id = getCurrentSc Ript (DOC); var module = modules [id]? Modules [id]: getEmptyModule (); module ['factory '] = factory; modules [id] = module; for (var I = 0; I