javascript - 允许 Javascript 模块(类)与 CommonJS、AMD 或两者都不一起使用

标签 javascript amd commonjs

我创建了一个简单的 Javascript 模块。我希望它可用于 CommonJS 实现、AMD 实现和全局 ()

这是我的类(class):

function Geolocation( callback ){
    this._latitude          = null;
    this._longitude         = null;
    this._accuracy          = null;
}

Geolocation.prototype = {
    //prototype definitions in here
}

我想要实现的目标可能吗?彻底的谷歌搜索没有产生任何结果

最佳答案

(function (global, factory) {
    if (typeof define === "function" && define.amd) define(factory); //AMD
    else if (typeof module === "object") module.exports = factory(); //CommonJS
    else global.Geolocation = factory(); // for example browser (global will be window object)
}(this, function () {

var Geolocation = function( callback ){
    this._latitude          = null;
    this._longitude         = null;
    this._accuracy          = null;
}

Geolocation.prototype = {
    //prototype definitions in here
}

return Geolocation;

}));

关于javascript - 允许 Javascript 模块(类)与 CommonJS、AMD 或两者都不一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33522339/

相关文章:

javascript - 使用 requirejs 和 r.js 优化器时无法加载 jQuery 插件

javascript - Browserify:如何避免两个 jQuery 实例?

javascript - 使用 index.ts 导出 'default'

JavaScript 内存释放

javascript - 在 Node 中链接文件以供下载 - Javascript、EJS

javascript - 将非输入元素与 Backbone.Syphon 一起使用

javascript - IE 在打印时删除网页颜色

requirejs - 如何加载在单个文件中定义的多个命名 AMD 模块?

javascript - 如何使用 webpack (sourcemap) 获得更具可读性的 bundle.js?

javascript - AMD 加载程序如何在后台工作?