Opening! Design and Implementation of JavaScript AMD Module

Source: Internet
Author: User

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
 
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.