Sunday 15 May 2011

JavaScript - Common Function to check Undefiend and Null -



JavaScript - Common Function to check Undefiend and Null -

i have number of variables in script, each of contain value, undefined or null.

i've noticed alot of repetative code checking undefined , nulls, can mutual function created?

i have created mutual fucntion check variables not undefined or null:

js original:

var testa; var testb; var testc; if ((typeof testa == "undefined" || testa == null) && (typeof testa == "undefined" || testb == null) && (typeof testc == "undefined" || testc == null)) { //do /////////// }

js new:

var testa; var testb; var testc; if (checkvalues(testa) && checkvalues(testb) && checkvalues(testc)) { //do /////////// } function checkvalues (v) { homecoming (v == "undefined") || (v == null); }

which works. if testa contains value, , testb , testc null or undefiend? e.g:

if ((typeof testa == "undefined" || testa == null) && (typeof testa != "undefined" || testb != null) && (typeof testc != "undefined" || testc != null)) { //do /////////// }

this depends on use, according given status "what if testa contains value, , testb , testc null or undefined" per status homecoming false.

javascript

No comments:

Post a Comment