Skip to content

Instantly share code, notes, and snippets.

@luozhihua
Last active August 29, 2015 13:57
Show Gist options
  • Save luozhihua/9406509 to your computer and use it in GitHub Desktop.
Save luozhihua/9406509 to your computer and use it in GitHub Desktop.
How to define an universal module?
(function(global) {
function Person() {
this.name = 'luozhihua';
this.company = 'tbc.com';
}
Person.prototype = {
say : function(words) {
alert(words || "I can't say any more, you know that.");
},
set: function(key, value) {
if (typeof key === 'string') {
this[key] = value;
}
},
get: function(key) {
return this[key];
}
}
// define as an amd module (require js)
if (typeof global.define === 'module') {
define([], function() {
return Person;
});
// define as a cmd module (seajs or nodejs)
} else if (module) {
module.exports = Person;
// define as a jquery plugins
} else if (global.jQuery) {
global.jQuery.fn.Person = Person;
// define as a global variable
} else {
global.Person = Person;
}
}(this));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment