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. Found this
solution at http://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;
}
}
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.'
I now know that this is bad practice, but don't tell me it's not a
common mistake.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---