dgmike
7/30/2014 - 11:26 AM

js: passing optional arguments

js: passing optional arguments

var cb, republish;

republish = function(message, update, queue, callback) {
  if (typeof update === "function") {
    callback = update;
    queue = update || queue;
    update = void 0;
  }
  if (typeof queue === "function") {
    callback = callback || queue;
    queue = void 0;
  }
  return {
    message: message,
    update: update,
    queue: queue,
    callback: callback
  };
};

cb = function() {};

console.clear();

console.table({
  one: republish('msg'),
  two: republish('msg', cb),
  three: republish('msg', {}, cb),
  four: republish('msg', {}, 'queue', cb)
});