Last active
August 29, 2015 13:57
-
-
Save luozhihua/9406509 to your computer and use it in GitHub Desktop.
How to define an universal module?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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