[Javascript] Object Not Defined
Terry Riegel
riegel at clearimageonline.com
Sun Nov 20 12:08:30 CST 2011
The point is I would like to test for the *existence* of an object of some
arbitrary level a.b.c.d.e.f.title and not have a mess of code to do so.
Currently the code would look like this.
if( a && a.b && a.b.c && a.b.c.d && a.b.c.d.e && a.b.c.d.e.f &&
a.b.c.d.e.f.title ) { return a.b.c.d.e.f.title } else { return 'untitled' }
What I would prefer is more readable code like this...
if( exist(a.b.c.d.e.f.title) ) {return a.b.c.d.e.f.title} else {return
'untitled'}
Unfortunately the second form makes sense but isn't valid. Is there a way
to make a function exist() that would make it valid?
Terry
On Nov 19, 2011, at 9:58 PM, Troy III Ajnej <trojani2000 at hotmail.com> wrote:
Hi one thing I don't understand is why are you returning an (object by
default) instead of (one would expect) a boolean, -logically a false
one!But that's probably because of your sepcific algo requirements there.
For this part of an a conditional:> if(a && a.data && a.data.items){ I
presume that this :if (typeof a!='undefined'){return a.data||[]...
would do just fine. You can however write a ternary assignment if you
like: var zeeData = (typeof a!='undefined') ? a.data.items||a.data:[] or
you can also have a function + arguments working their ass for you:
function(a, data, item ){ return( typeof a != 'undefined' ) ?
a[data][item] || a[data] || [] : [];}
!This will retrieve an altrenative, in case a.data.item is not presentyou
will recieve the "a.data" content instead of empty [] if it exists, or if
that's not of any (I presume) practical use for you, -you can: function(a,
data, item ){ return( typeof a != 'undefined' ) ? a[data] ?
a[data][item] : [] : [];}
Or perhaps: function zeeData(a, data, items ){
return( typeof a & a[data] != 'undefined' ) ? a[data][items] || [] : [ ];
}
since if I understood it well - you are targeting the third row,this would
be a better choice, or my final thought on this turn I guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Troy III
progressive art enterprise
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: riegel at clearimageonline.com
Date: Fri, 11 Nov 2011 07:45:07 -0500
To: javascript at lists.evolt.org
Subject: [Javascript] Object Not Defined
Hello All,
I am looking for a better way for the following piece of code...
function(a){
if(a && a.data && a.data.items){
return a.data.items;}
else{
return[];
}
}
It would be nice if this did the same thing...
function(a){return a.data.items || []};
Anyone have any thought on how this could be done I wouldn't mind wrapping
it into a function if that helped. Something like this possibly...
function(a){return or(a.data.items,[]) };
Thanks for any help.
Terry
_______________________________________________
Javascript mailing list
Javascript at lists.evolt.org
http://lists.evolt.org/mailman/listinfo/javascript
_______________________________________________
Javascript mailing list
Javascript at lists.evolt.org
http://lists.evolt.org/mailman/listinfo/javascript
More information about the Javascript
mailing list