zoukankan      html  css  js  c++  java
  • CommonJs AMD CMD 模块加载

    CommonJs 定义的是同步模块加载方案,AMD/CMD定义的是异步模块加载方案

    CommonJs:  包含两大工具 require() 以及 module.exports

          module.exports 用于产生一个模块,方法是将在文件末尾加入 module.exports = 该文件内的一个变量,就使得该变量成为模块

          require 用于引用一个模块,例如 require("./helllo") 括号内为路径名并且不要".js"的后缀

          

    //举例
    // moduleA.js
    module.exports = function( value ){
        return value*2;
    }
    
    // moduleB.js
    var multiplyBy2 = require('./moduleA');
    var result = multiplyBy2( 4 );

    AMD  RequireJS 在推广过程中对模块定义的规范化产出

        - define(id?, dependencies?, factory); 

          - id 可选参数,字符串类型。独一无二,id即为模块的id.如果id参数存在,那么id参数必须是顶层或者绝对id(而不是相对id)

          - dependencies 可选参数,id列表,用于指出该模块的依赖模块,依赖模块的id可能是相对id。如果dependencies参数不存在,模块加载器有可能扫描factory函数来获得相关的依赖模块。当模块加载器加载该模块时,会先加载依赖模块

          - factory 必要参数,函数类型或者object类型。若为函数类型,该函数只能被执行一次,若为object类型,则应该作为模块的输出

        

    
    

      Sets up the module with ID of "alpha", that uses require, exports and the module with ID of "beta":

    
    
       define("alpha", ["require", "exports", "beta"], function (require, exports, beta) {
           exports.verb = function() {
               return beta.verb();
               //Or:
               return require("beta").verb();
           }
       });

    An anonymous module that returns an object literal: define([
    "alpha"], function (alpha) { return { verb: function(){ return alpha.verb() + 2; } }; }); A dependency-free module can define a direct object literal: define({ add: function(x, y){ return x + y; } });

    加载方法

      require(['math'], function (math){

    
    

        alert(math.add(1,1));

    
    

      });

    CMD  CMD定义规范中一个模块就是一个文件。代码书写格式:

        更多请看: https://github.com/seajs/seajs/issues/242

        define(id?, deps?, factory)

        require(id)

        require.async(id, callback?)

        require.resolve(id)

      

  • 相关阅读:
    X 如何理解关系型数据库的常见设计范式?
    X 使用DMV,诊断和调优DB性能。
    X MSSQL-并发控制-2-Isolation msql 的各种隔离级别 sqlserver
    X SQL Server AG集群启动不起来的临时自救大招
    X 搭建非域AlwaysOn win2016+SQL2016
    X 从0开始搭建SQL Server AlwaysOn 第四篇(配置异地机房节点)
    X 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
    X 从0开始搭建SQL Server AlwaysOn 第二篇(配置故障转移集群)
    Python Cookie Session和分页
    Python django应用之corsheaders[跨域设置]
  • 原文地址:https://www.cnblogs.com/lifeisshort/p/4905617.html
Copyright © 2011-2022 走看看