0
Understand if the variable is valid
What kind of control do you use to understand if the variable is valid (not undefined, not null, not empty)? Something like this : hasAValue: function (value) { return (("" !== value) && (null !== value) && ("undefined" !== typeof value) ); },
3 ответов
+ 3
(value) produce already true or false, so:
if (value) { };
... is strictly equivaent to:
if hasValue(value) { }
The only difference being add a call to a function, a test, and context ( variables scope ), as much unnecessary time and memory requirement ^^
0
Yes, but order can be improved by:
- putting the typeof statement comparison at first, so if it is you don't need to perform others comparisons
- condensing your strict (not) equality tests of 'null' and 'undefined' into once not strict, which is equivalent
return ( (null != value) && ("" !== value) );
But you can simply test if the variable has a value which is NOT in:
- null
- undefined
- NaN
- 0
- false
-""
by:
return ( value );
Well, so... you don't need anymore of a function for that :P
0
like this :
hasAValue: function (value) {
if(value){
return true;
} else {
return false;
}
}