Baris Bikmaz wrote:
> I prefer the way Peter proposed by checking the property with a
> function, like in other languages. It's quite more readable.
> And I prefer not to write the properties again after checking them so a
> quite similar approach were to write a method
> like that:
>
> Object.prototype.hasCustomProperty = function( sProperty ) {
> var arr = sProperty.split('.');
> var result = this;
>
> var i = 1;
> var l = arr.length;
> while ( l && (result = result[arr.shift()]) ){
> if( i++ == l ) { return result; }
> }
> return false;
> }
This is fine as long as your property names do not contain periods.
And how would you deal with arrays? That is, how would you do the
equivalent of
`person.hasCustomProperty("person.friends[3].firstName")`?
Also, I don't think a property fetcher should have the name
`hasCustomProperty`. A function with that name should, IMHO, return
only `true` or `false`. And how do you distinguish between fetching a
non-existent property and one that happens to be false? I'd suggest
you return `undefined` or `null` rather than false.
-- 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]