-2

Just curious. Is there a simpler/better way to write the following code? I need just two arguments options and callback. The function may be called without any argument, with just callback, or with an options hash and callback.

function () {
  if (typeof arguments[0] === "function") {
    callback = arguments[0];
  } else if (arguments[0] && typeof arguments[0] === "object" && typeof arguments[1] === "function") {
    options = arguments[0];
    callback = arguments[1];
  }
  ...
  ...

}
Geordee Naliyath
  • 1,799
  • 17
  • 28

1 Answers1

2
function (options, callback) {
  if (typeof options === "function") {
    callback = options;
    options = {};
  }
  // code
}
Mykyta Shyrin
  • 312
  • 1
  • 14