This is Devhints.io cheatsheets — a collection of cheatsheets I've written.

# Universal JS module loader

### With dependency

    ;(function (root, factory) {

      if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);
      } else if (typeof exports === 'object') {
        module.exports = factory(require('jquery'));
      } else {
        root.YourModule = factory(root.jQuery);
      }

    }(this, function (jquery) {
      return {};
    }));

### No dependencies

    ;(function (root, factory) {

      if (typeof define === 'function' && define.amd) {
        define(factory);
      } else if (typeof exports === 'object') {
        module.exports = factory();
      } else {
        root.YourModule = factory();
      }

    }(this, function () {
      return {};
    }));

### Supports circular references

    (function (root, factory) {

      if (typeof define === 'function' && define.amd) {
        define(['exports', 'jquery'], factory);
      } else if (typeof exports === 'object') {
        factory(exports, require('jquery'));
      } else {
        factory((root.YourModule = {}), root.jQuery);
      }

    }(this, function (exports, jQuery) {
      exports.action = function () {};
    }));

### Reference

- https://github.com/umdjs/umd

Devhints.io cheatsheets is a collection of cheatsheets I've written over the years. Suggestions and corrections? Send them in. I'm Rico Sta. Cruz. Check out my Today I learned blog for more.