javascript - What is the best way to check that multiple variables are set? -
this more of general practices question.
the language working javascript. have function getting object many variables (okay, 10 variables). best way make sure function getting required variables , set?
i know, know, why not use if
statement. that's big chunk of if statements! grow programmer, know may not best method this. i'm looking shortcut actually. how you check large sum of variables existence , non-blank values?
this expression returns true, if variables variablenamelist
(list of required variable names) set in object o
:
variablenamelist.every(function(varname){ return typeof o[varname] !== 'undefined'; });
you can use underscore _.all
function instead of native every
, , underscore _.isundefined
instead of typeof ...
.
Comments
Post a Comment