Skip to content

Instantly share code, notes, and snippets.

@totty90
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save totty90/106f9577482154f9ab4e to your computer and use it in GitHub Desktop.

Select an option

Save totty90/106f9577482154f9ab4e to your computer and use it in GitHub Desktop.
Requirejs and Commonjs crossmodule standard boilerplate
(typeof define==="function"?define:function(f){module.exports=f(require,exports,module)})(function(require,exports,module){
return {myModule: 5};
// require('someModule').myModule === 5;
// or
return function(a){return a};
// require('someModule')(3) === 3;
// or
var A = function(a){this.a = a};
A.prototype.xxx = function(){return this.a};
return A;
// var a = new require('someModule')(1)
// a.xxx() === 1;
//
// with requirejs
// define(['someModule'], function(someModule){
// var a = new someModule(1);
// a.xxx() === 1;
// })
})
@totty90
Copy link
Author

totty90 commented Jul 11, 2014

Module we are making:

  • moduleA.js: returning the module;
  • moduleB.js: setting the module.exports variable;

Both way works the same. But remember to don't do both: you should not return a module and also set the module.exports variable;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment