The downside of always using an object literal to pass parameters is that the burden of remembering parameter names is shifted to the developer and is repeated with every use (nevermind how your code size would increase if every function worked this way). That's why I like to only use this pattern for optional parameters while required parameters, as well as simple parameters, remain as named arguments.

-Nicholas

_____________________________________________________
Nicholas C. Zakas
Twitter: @slicknet
Blog: http://www.nczonline.net/

-----Original Message----- From: Scott Sauyet
Sent: Wednesday, January 19, 2011 7:49 PM
To: The JSMentors JavaScript Discussion Group
Subject: [JSMentors] Re: JS API Design - Accepting Parameters

Andraž Kos wrote:
There is a simpler common sense reason to always use an Object for all
params: [ ... ]

runme(args) can be called:

runme({'x':bar,'y':foo});

or

runme({'y':foo,'x':bar}); [ ... ]

Yes, and for most APIs this is fine.  But let's not forget that there
are exceptions.  Sometimes a particular order of parameters is
intrinsically clear.  For instance,

   var p1 = new Point(2, 7)

is shorter and probably clearer than

   var p2 = new Point({x: 2, y:7});

And there is no reason that we should want to rewrite Math.pow like
this:

   var kb = Math.pow({base: 2, exponent: 10});

But such are exceptions.  The rule is still solid.

 -- Scott

--
To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com
--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to