Michael Haufe (TNO) wrote:
>Sam Merrell wrote:
>>     var person = {address: {zip: 1234}},
>>          person2 = {};
>>     console.log("Person2 undefined: " +
>> ((person2||{}).address||{}).zip || "no zip");
>
> Looks like a code smell of bad design. If you have a Person object it
> would be unnecessary to do member sniffing tests and literal
> injections like this.
> [ ... ]
> I'd say do this:
>
> var person = new Person(new Address(1234)),
>      person2 = new Person();
> console.log(person2.zip || "no zip");

I don't buy it.  Presumably you have an Address object nested in your
Person object that itself has a `zip` property.  And if that's the
case, you still need to reference it by

    person.address.zip

which could still have the problem we're trying to address, unless you
are automatically creating empty objects for every missing property.
I can't see what it buys us.

I'm guessing that what Angus was suggesting with

| Michael, If you've gone that far why not just have Person have a
| getZip() method to encapsulate the null checking?

is the same thing I feel:  you're trying to cram a Java/C# OO idiom
into Javascript which more naturally deals with very dynamic data
structures.

  -- Scott

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to