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

Allowing a Javascript module (class) to be used with CommonJS, AMD or neither

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

这是我的 class:

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

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

我想要实现的目标可行吗?详尽的 google 搜索未产生任何结果

(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;

}));