Multiple arguments vs. options object

When creating a JavaScript function with multiple arguments, I am always confronted with this choice: pass a list of arguments vs. pass an options object.

For example I am writing a function to map a nodeList to an array:

function map(nodeList, callback, thisObject, fromIndex, toIndex){
    ...
}

I could instead use this:

function map(options){
    ...
}

where options is an object:

options={
    nodeList:...,
    callback:...,
    thisObject:...,
    fromIndex:...,
    toIndex:...
}

Which one is the recommended way? Are there guidelines for when to use one vs. the other?

[Update] There seems to be a consensus in favor of the options object, so I’d like to add a comment: one reason why I was tempted to use the list of arguments in my case was to have a behavior consistent with the JavaScript built in array.map method.

10 Answers
10

Leave a Comment