Friday, 15 July 2011

javascript - Shorthand way to check for function parameters -



javascript - Shorthand way to check for function parameters -

in code, have function creates new map given few parameters. want create if no parameters passed, default values used.

why won't work:

function create(a,b,c) { homecoming new map(a,b,c || 10,1,10); // create new map using b c parameters // or 10, 1, 10 if none entered. } create();

assume there constructor function 'map' take , process these parameters.

what can besides having if/else type check?

the shortest way know under limitations use

<var> = <var> || <defaultvalue>;

so

return new map((a = || 10), (b = b || 1), (c = c || 10));

however way hardly readable , might want consider moving assignments before constructor.

the limitation falsy values lead default beingness assigned might problem numeric values.

javascript syntax

No comments:

Post a Comment