《返回

AMD/commonJs兼容写法

模块定义
(function() {
  var Obj = {
    name: "DH",
    say: function() {
      console.log("你好")
    }
  }
  if (typeof module !== "undefined" && typeof exports === "object") {
    module.exports = Obj;
  } else if (typeof define === "function" && (define.amd || define.cmd)) {
    define(function() {
      return Obj;
    });
  } else {
    this.Obj = Obj;
  }
}).call(this || (typeof window !== "undefined" ? window : global));
config.js配置
require.config({    
  paths: {      
    "commonjs": "./assets/js/commonjs"
  }
});
模块调用
requirejs(["commonjs"], function(o) {
   console.log(o.name);
   o.say();
});