On Nov 21, 9:56 pm, "stijn.m" <[EMAIL PROTECTED]> wrote:
> On 21 nov, 10:58, RobG <[EMAIL PROTECTED]> wrote:
>
> > Use the built-in Object.prototype.hasOwnProperty method.
>
> Okay. Thanks, this is great help. I knew it wasn't good practice, but
> needed a quick solution (no time to think). This one is better:
>
> Dropped the 'isValue' function and the whole sniffing thing going on,
> doing it like this now:
>
> for(i in arr) if(!arr.hasOwnProperty || arr.hasOwnProperty(i)) {
> // do something ...
> }
>
> Mind you, some browsers (Safari) don't support the method.
As a general statement, that is wrong. Safari has supported
hasOwnProperty since version 2.0.2. Version 2.0 was released with Mac
OS 10.4, anyone who has kept that OS updated (with free updates) will
be running version 3.0.4. The previous update version was 2.0.4,
Safari on Windows started at version 3.
> Found this
> solution athttp://erik.eae.net/archives/2005/06/06/22.13.54/
>
> if(!Object.prototype.hasOwnProperty) {
> Object.prototype.hasOwnProperty = function(property) {
> try {
> var prototype = this.constructor.prototype;
> while(prototype) {
> if(prototype[property] == this[property]) return false;
> prototype = prototype.prototype;
> }
> }
> catch(e) {}
> return true;
> }
>
> }
I don't like stuff that unneccessarily uses try..catch. Consider the
following from Rails Trac:
<URL: http://dev.rubyonrails.org/attachment/ticket/9700/hasOwnProperty.diff
>
>
> As for this:
>
> > Javascript does not have associative arrays, it has objects.
>
> Basically learned JavaScript from the book. I quote from 'Inside
> JavaScript ed. 2003' (Steven Holzner):
>
> 'In JavaScript, you don't have to use index numbers in arrays; you can
> also use index strings ... Arrays that use strings as indices for data
> like this are also called associative arrays.'
Associative arrays usually have many more characteristics than named
properties that use strings, or numeric string indices.
> I now know that this is bad practice, but don't tell me it's not a
> common mistake.
That would appear to be a very misleading statement, I can't comment
further as I don't have the full context. It should have said
"...inappropriately called associative arrays".
The numeric indices in javascript Arrays are *all* strings, as Object
property names must be strings. Javascript Arrays are Objects with a
couple of special properties and a bunch of extra methods, properties
can be added in exactly the same way as for any other Object. It is
because Arrays have Object.prototype on their prototype chain (i.e.
are "instances" of Object) that extending Object.prototype creates the
issue you are trying to resolve.
But anyway, good to see you've found a better solution. :-)
--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---