The problem is that passing the variable undefined to the function causes an error.
This means that you need to run typeof before passing it as an argument.
The cleanest way I found for this is as follows:
function isset(v){ if(v === 'undefined'){ return false; } return true; }
Using:
if(isset(typeof(varname))){ alert('is set'); } else { alert('not set'); }
Now the code is much more compact and readable.
This will still give an error if you try to call a variable from a non-specific variable, for example:
isset(typeof(undefVar.subkey))
before trying to accomplish this, you need to make sure that the object is defined:
undefVar = isset(typeof(undefVar))?undefVar:{};
Dieter Gribnitz Dec 19 '13 at 8:23 2013-12-19 08:23
source share