Skip to content

Instantly share code, notes, and snippets.

@paton
Last active October 7, 2019 16:14
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save paton/ab27a1be7e843d220ee3 to your computer and use it in GitHub Desktop.
Save paton/ab27a1be7e843d220ee3 to your computer and use it in GitHub Desktop.
Super simple implementation of define() and require() used in Localize.js (https://localizejs.com)
var define, require;
(function() {
var modules = {};
require = function(name) {
return modules[name]();
};
define = function(name, fn) {
var exports;
modules[name] = function() {
if (!exports) {
exports = {};
exports = fn(require, exports) || exports;
}
return exports;
};
};
})();
@paton
Copy link
Author

paton commented Nov 3, 2014

Example usage:

define('amazing', function(require, exports) {
  exports.magic = function() {
    return Math.random();
  }
});

define('moreAmazing', function(require, exports) {
  var amazing = require('amazing');

  exports.somethingCool = function() {
    return amazing.magic()
  }
});

var moreAmazing = require('moreAmazing');
console.log(moreAmazing.somethingCool());

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