I know of two methods of setting a default parameter, but I would like to know what the preferred method is.
function Foo(par1, par2) { if(par2 == null) par2 = 'my default' }
or
function Foo(par1, par2) { par2 = par2 || 'my default' }
or is there a better way than either of these?
EDIT:
I would also like to know how others handle multiple optional parameters such as this: We have several functions like this in internal libraries (I think they’re pretty ugly).
function Foo(par1, par2, par3) { if(par2 == null) par2 = 'my default' if(par3 == null) par3 = 'my default' // Do something }
And to call it:
Foo('Parameter one',null,true)
Personally I think the second form is just outright cleaner and more readable, certainly moreso than sergey’s arguments inspection – though that can have it’s place – but I prefer to pass extensible objects for args than have to maintain arg signatures.
e.g.