允许Javascript模块(类)与CommonJS,AMD或两者兼用

时间:2015-11-04 12:46:08

标签: javascript amd commonjs

我创建了一个简单的Javascript模块。我希望CommonJS实现,AMD实现和global()

可以使用它

这是我的班级:

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

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

我想要实现的目标是什么?详尽的谷歌搜索没有产生任何结果

1 个答案:

答案 0 :(得分:1)

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

}));