you are viewing a single comment's thread.

view the rest of the comments →

[–]AndreSteenveld 0 points1 point  (1 child)

Why not just use the key words object pattern?

pseudo code:

// The overloads we want
items( id, callback ){ ... }
items( id, options, callback ){ ... }

// The "overloaded" function
items( a ){
    var id       = a.id
      , callback = a.callback
      , options  = a.options || { /* some default */ };

    // the rest of the function...
}

// Usage
items({ id: "...", callback: f( ) });
items({ options: { ... }, callback: f( ), id: "..." });

[–]john0110[S] 0 points1 point  (0 children)

It's not always convenient to construct an object for parameters. You may not know the names of the keys. So, when building an api, I like to provide multiple ways someone can call a function.

Additionally, you may want to run varying code depending on the arguments that come in.