Passing named arguments to a Javascript function -
this question has answer here:
- named parameters in javascript 8 answers
calling javascript function like
somefunction(1, true, 'foo');
is not clear without familiarity function.
i have seen , used style comments inserted name arguments:
somefunction(/*itemstoadd*/1, /*displaylabel*/ true, /*labeltext*/ 'foo');
but when gets beyond 3 or more parameters, seems better pass arguments in json object makes order-independent, , allows default values provided in called function
somefunction({'itemstoadd':1, 'labeltext':'foo', 'displaylabel':true});
my question is; general practice in industry, , there overriding reasons not using of these methods. lint example not second method.
personally i'd grep function name , take @ comment associated it. maintained code have comment above function explaining arguments , them, , can paste above function call if need explain why arguments way are.
using json pass arguments seems way add unnecessary parsing overhead , possibly confuse maintainer - add more fields , pass in nulls fields want default values, , can explain why you're passing in nulls in call comment instead of having them not appear in json.
Comments
Post a Comment