⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 display.php-n=10-5&f=10-module.js.htm

📁 javascript 5 随书源码 javascript
💻 HTM
📖 第 1 页 / 共 2 页
字号:
    // Register this namespace in the map of all modules    Module.modules[name] = namespace;    // Return the namespace object to the caller    return namespace;}/** * Test whether the module with the specified name has been defined. * Returns true if it is defined and false otherwise. */Module.isDefined = function(name) {    return name in Module.modules;};/** * This function throws an error if the named module is not defined * or if it is defined but its version is less than the specified version. * If the namespace exists and has a suitable version, this function simply * returns without doing anything. Use this function to cause a fatal * error if the modules that your code requires are not present. */Module.require = function(name, version) {    if (!(name in Module.modules)) {        throw new Error("Module " + name + " is not defined");    }    // If no version was specified, there is nothing to check    if (!version) return;    var n = Module.modules[name];    // If the defined version is less than the required version or of    // the namespace does not declare any version, throw an error.    if (!n.VERSION || n.VERSION &lt; version)    throw new Error("Module " + name + " has version " +                    n.VERSION + " but version " + version +                    " or greater is required.");};/** * This function imports symbols from a specified module.  By default, it * imports them into the global namespace, but you may specify a different * destination as the second argument. * * If no symbols are explicitly specified, the symbols in the EXPORT * array of the module will be imported.  If no such array is defined, * and no EXPORT_OK is defined, all symbols from the module will be imported.  * * To import an explicitly specified set of symbols, pass their names as * arguments after the module and the optional destination namespace. If the * modules defines an EXPORT or EXPORT_OK array, symbols will be imported * only if they are listed in one of those arrays. */Module.importSymbols = function(from) {    // Make sure that the module is correctly specified.  We expect the    // module's namespace object but will try with a string, too    if (typeof from == "string") from = Module.modules[from];    if (!from || typeof from != "object")        throw new Error("Module.importSymbols(): " +                         "namespace object required");    // The source namespace may be  followed by an optional destination    // namespace and the names of one or more symbols to import;    var to = Module.globalNamespace; // Default destination    var symbols = [];                // No symbols by default    var firstsymbol = 1;             // Index in arguments of first symbol name    // See if a destination namespace is specified    if (arguments.length &gt; 1 &amp;&amp; typeof arguments[1] == "object") {        if (arguments[1] != null) to = arguments[1];         firstsymbol = 2;    }    // Now get the list of specified symbols    for(var a = firstsymbol; a &lt; arguments.length; a++)        symbols.push(arguments[a]);        // If we were not passed any symbols to import, import a set defined    // by the module, or just import all of them.    if (symbols.length == 0) {        // If the module defines an EXPORT array, import        // the symbols in that array.        if (from.EXPORT) {            for(var i = 0; i &lt; from.EXPORT.length; i++) {                var s = from.EXPORT[i];                to[s] = from[s];            }            return;        }        // Otherwise if the modules does not define an EXPORT_OK array,        // just import everything in the module's namespace        else if (!from.EXPORT_OK) {            for(s in from) to[s] = from[s];            return;        }    }    // If we get here, we have an explicitly specified array of symbols    // to import. If the namespace defines EXPORT and/or EXPORT_OK arrays,    // ensure that each symbol is listed before importing it.    // Throw an error if a requested symbol does not exist or if     // it is not allowed to be exported    var allowed;    if (from.EXPORT || from.EXPORT_OK) {        allowed = {};        // Copy allowed symbols from arrays to properties of an object.        // This allows us to test for an allowed symbol more efficiently.        if (from.EXPORT)             for(var i = 0; i &lt; from.EXPORT.length; i++)                allowed[from.EXPORT[i]] = true;        if (from.EXPORT_OK)            for(var i = 0; i &lt; from.EXPORT_OK.length; i++)                allowed[from.EXPORT_OK[i]] = true;    }        // Import the symbols    for(var i = 0; i &lt; symbols.length; i++) {        var s = symbols[i];              // The name of the symbol to import        if (!(s in from))                // Make sure it exists            throw new Error("Module.importSymbols(): symbol " + s +                            " is not defined");        if (allowed &amp;&amp; !(s in allowed))  // Make sure it is a public symbol            throw new Error("Module.importSymbols(): symbol " + s +                            " is not public and cannot be imported.");        to[s] = from[s];                 // Import it    }};// Modules use this function to register one or more initialization functionsModule.registerInitializationFunction = function(f) {    // Store the function in the array of initialization functions    Module._initfuncs.push(f);    // If we have not yet registered an onload event handler, do so now.    Module._registerEventHandler();}// A function to invoke all registered initialization functions.// In client-side JavaScript, this will automatically be called in// when the document finished loading.  In other contexts, you must// call it explicitly.Module.runInitializationFunctions = function() {    // Run each initialization function, catching and ignoring exceptions    // so that a failure by one module does not prevent other modules    // from being initialized.    for(var i = 0; i &lt; Module._initfuncs.length; i++) {        try { Module._initfuncs[i](); }        catch(e) { /* ignore exceptions */}    }    // Erase the array so the functions are never called more than once.    Module._initfuncs.length = 0;}// A private array holding initialization functions to invoke laterModule._initfuncs = [];// If we are loaded into a web browser, this private function registers an // onload event handler to run the initialization functions for all loaded // modules. It does not allow itself to be called more than once.Module._registerEventHandler = function() {    var clientside =   // Check for well-known client-side properties        "window" in Module.globalNamespace &amp;&amp;        "navigator" in window;    if (clientside) {        if (window.addEventListener) {  // W3C DOM standard event registration            window.addEventListener("load", Module.runInitializationFunctions,                                     false);        }        else if (window.attachEvent) {  // IE5+ event registration            window.attachEvent("onload", Module.runInitializationFunctions);        }        else {            // IE4 and old browsers            // If the &lt;body&gt; defines an onload tag, this event listener            // will be overwritten and never get called.            window.onload = Module.runInitializationFunctions;        }    }    // The function overwrites itself with an empty function so it never    // gets called more than once.    Module._registerEventHandler = function() {};}</pre><div class="lefttitle" style="padding:0px; text-align: right"><a href="index.html" tppabs="http://www.davidflanagan.com/javascript5/index.html">Table of Examples</a></div><p><script type="text/javascript">amazon_ad_tag="davidflanagancom"; amazon_ad_width="300"; amazon_ad_height="250"; amazon_ad_logo="hide";</script><script type="text/javascript" src="../../www.assoc-amazon.com/s/ads.js" tppabs="http://www.assoc-amazon.com/s/ads.js"></script></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -