[Javascript] Object Not Defined
Troy III Ajnej
trojani2000 at hotmail.com
Sun Nov 20 13:30:35 CST 2011
I see what you mean...
I think you'll need my >>isDeclared()<< method I've written sometime
ago for testing variable existence, that is: discern if a variable is
declared or undeclared in order to safely delete it as soon as I'm
done working with it, or programmaticaly declare a new one.
There's no need for:
> 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 ....
in case you are interested in directly obtaining a title value, you'll
go strait at: "a.b.c.d.e.f.title".
I think,
you would like something like:
---------------------------------------------------------------------
function getDSOvalue( dsoVal, default ){ return isDeclared(dsoVal) ? dsoVal : default }
getDSOvalue( "a.b.c.d.e.f.title", "untitled" );
---------------------------------------------------------------------
is this correct?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Troy III
progressive art enterprise
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> From: riegel at clearimageonline.com
> Date: Sun, 20 Nov 2011 13:08:30 -0500
> To: javascript at lists.evolt.org
> Subject: Re: [Javascript] Object Not Defined
>
> 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
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript
More information about the Javascript
mailing list