[Javascript] Object Not Defined

Terry Riegel riegel at clearimageonline.com
Sat Nov 12 20:25:14 CST 2011


how wise is it
to make assumptions about arbitrary objects?


It would be unwise. But testing for existence shouldn't look like spaghetti
either.

Perhaps the expected object is pulled from an external source outside of
the control of my application. There are many times I would like to simply
call...

a.b.c.title || 'untitled'

And not have to fiddle with the idea that some part of the chain will cause
a failure.

Terry

Sent from my iPhone

On Nov 12, 2011, at 8:59 PM, Mike Dougherty <mdougherty at pbp.com> wrote:

On Sat, Nov 12, 2011 at 10:51 AM, Terry Riegel
<riegel at clearimageonline.com> wrote:

I think this problem would be nice to have some sort of generalized

helper function. Perhaps...


if( exist(a.b.c.d.e.f) )


All the solutions I have seen seem to be long and error prone.


Do you find yourself writing the kind of code that expects arbitrary
parameters to deep-search a complex hierarchy without being sure the
property tree exists?

I doubt it's possible to write a smartlyHandleAnything(parameter)
function that does more than describe the parameter rather than
attempt to use it for anything.  If I'm proven wrong, how wise is it
to make assumptions about arbitrary objects?

Perhaps a solution would be to create a constructor function to
manufacture your complex objects, then in your  function/methods that
accept these objects you could check the passed object's .constructor
property and be reasonably sure the object is correct for the method.
(and throw an error for anything else passed incorrectly)

Of course that'll likely require rethinking your whole object creation
paradigm - ymmv  :)


http://skilldrick.co.uk/2011/09/understanding-typeof-instanceof-and-constructor-in-javascript/
_______________________________________________
Javascript mailing list
Javascript at lists.evolt.org
http://lists.evolt.org/mailman/listinfo/javascript


More information about the Javascript mailing list