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

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

本文关键字:AMD 两者都 CommonJS 模块 Javascript 允许      更新时间:2023-09-26

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

这是我的课:

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;
}));