fka blog journal of programming studies

Exporting the Common Module for Node.js and Native Browser, in a Short Way

Today, I saw a beautiful and different way to define common module for Node.js and the browser. In the leFunc.js’s source, there is a beautiful way to handle it.

var module  = module || {}
  , window  = window || module.exports || exports
  , exports = module.exports || exports || window
;

Update on 12 November 2012: leFunc replaced that method with a simpler one.

if (typeof module !== "undefined")
  module.exports.leFunc = leFunc;
else if (typeof define !== "undefined")
  define('leFunc', function(){ return leFunc });
else window.leFunc = leFunc;

Maybe, we can make it more modular:

function exporter(name, module) {
  if (typeof module !== "undefined")
    module.exports[name] = module;
  else if (typeof define !== "undefined")
    define(name, function(){ return module });
  else window[name] = module;
}

Have a good day! :)