Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Kit's Universal Module Pattern
(function (root, Library) {
// The square bracket notation is used to avoid property munging by the Closure Compiler.
if (typeof define == "function" && typeof define["amd"] == "object" && define["amd"]) {
// Export for asynchronous module loaders (e.g., RequireJS, `curl.js`).
define(["exports"], Library);
} else {
// Export for CommonJS environments, web browsers, and JavaScript engines.
Library = Library(typeof exports == "object" && exports || (root["Library"] = {
"noConflict": (function (original) {
function noConflict() {
root["Library"] = original;
// `noConflict` can't be invoked more than once.
delete Library.noConflict;
return Library;
}
return noConflict;
})(root["Library"])
}));
}
})(this, function (exports) {
// ...
return exports;
});
@michaelficarra

This comment has been minimized.

Show comment Hide comment
@michaelficarra

michaelficarra Sep 29, 2011

There should be no need to wrap the noConflict property's IIFE in parentheses because function declarations are not allowed in that context. Also, you can just return a noConflict named function expression directly instead of using a function declaration.

      "noConflict": function (original) {
        return function noConflict() {
          root["Library"] = original;
          // `noConflict` can't be invoked more than once.
          delete Library.noConflict;
          return Library;
        };
      }(root["Library"])

There should be no need to wrap the noConflict property's IIFE in parentheses because function declarations are not allowed in that context. Also, you can just return a noConflict named function expression directly instead of using a function declaration.

      "noConflict": function (original) {
        return function noConflict() {
          root["Library"] = original;
          // `noConflict` can't be invoked more than once.
          delete Library.noConflict;
          return Library;
        };
      }(root["Library"])
@kitcambridge

This comment has been minimized.

Show comment Hide comment
@kitcambridge

kitcambridge Sep 29, 2011

@michaelficarra Indeed, the extra parentheses are a stylistic preference. Returning a named function expression will throw a syntax error in Safari 2, though. It's an outdated browser, but I strive to support it where possible.

Owner

kitcambridge commented Sep 29, 2011

@michaelficarra Indeed, the extra parentheses are a stylistic preference. Returning a named function expression will throw a syntax error in Safari 2, though. It's an outdated browser, but I strive to support it where possible.

@jrburke

This comment has been minimized.

Show comment Hide comment
@jrburke

jrburke Oct 7, 2011

@kitcambridge, I am likely going to advocate something like this gist for the AMD folks. If you see any weaknesses, please feel free to let me know.

jrburke commented Oct 7, 2011

@kitcambridge, I am likely going to advocate something like this gist for the AMD folks. If you see any weaknesses, please feel free to let me know.

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