Created
September 6, 2011 18:10
-
-
Save ryanflorence/1198466 to your computer and use it in GitHub Desktop.
Universal JavaScript Module, supports AMD (RequireJS), Node.js, and the browser.
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 (name, definition){ | |
if (typeof define === 'function'){ // AMD | |
define(definition); | |
} else if (typeof module !== 'undefined' && module.exports) { // Node.js | |
module.exports = definition(); | |
} else { // Browser | |
var theModule = definition(), global = this, old = global[name]; | |
theModule.noConflict = function () { | |
global[name] = old; | |
return theModule; | |
}; | |
global[name] = theModule; | |
} | |
})('myModule', function () { | |
// return the module's API | |
return {}; | |
}); |
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
// AMD | |
require(['path/to/myModule'], function (myModule){ | |
// use myModule here | |
}); | |
// Node.js | |
var myModule = require('myModule'); | |
// Global | |
myModule | |
// if myModule is already defined, `noConflict` gives it back | |
var myNonConflictingModule = myModule.noConflict(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm playing around with a format here: https://gist.github.com/1262861
It puts most of the nasty adapter stuff at the bottom, so readers of the code can focus first on the module's capabilities and not the registration boilerplate. It is a bit opinionated though.