js模块的写法

// 模块化写法一
;(function(global) {
  var factory = function() {
    var moduleName = {}
    return moduleName
  }

  if (typeof module !== 'undefined' && typeof exports === 'object') {
    module.exports = factory()
  } else if (typeof define === 'function' && (define.cmd || defind.amd)) {
    defind(factory)
  } else {
    global.moduleName = factory()
  }
})(typeof window !== 'undefined' ? window : global)

// 模块化写法二
;(function(global, factory) {
  if (typeof module !== 'undefined' && typeof exports === 'object') {
    module.exports = factory()
  } else if (typeof defind === 'function' && (defind.cmd || defind.amd)) {
    defind(factory)
  } else {
    global.moduleName = factory()
  }
})(typeof window !== 'undefined' ? window : global, function() {
  var moduleName = {}
  return moduleName
})

你可能感兴趣的:(js模块的写法)