Re: Storage 'length' and enumeration

On Wed, 29 Apr 2009, John J. Barton wrote:
> 
>> You only have to think hard about this if you use numeric keys and use 
>> the implied accessors. Just use .setItem("2") and .getItem("2"), or use 
>> a non-numeric key, and then there's no confusion.
> 
> So why is numeric keys part of the API?

There are no limits on the keys because we don't want to prevent someone 
from just using any random scheme for their keys if they want to.


> What would you recommend to use for the right hand side of "instanceof"? 
> Recall the goal here is to provide a visual representation of an object, 
> any object in any Javascript program what so ever.

For Storage objects, I would do an instanceof against the Storage 
prototype. There can't be other objects that implement that interface.


> But in the context of Storage API, what do I conclude? That Storage 
> should use length because it is like a TimeRange? As I said before, the 
> mistakes of the past should not be repeated.

I don't consider it a mistake.


On Thu, 30 Apr 2009, Sean Hogan wrote:
>
> I'm not sure about all those objects, but my (incomplete) testing of
> HTML*Collections indicates that when accessed using array notation:
>    object[index]; where index < object.length
> they behave as a readonly array. Which has the fortuitous by-product of
> allowing the object to work with Array.forEach(), etc.
> 
> Storage objects have a length property and can be accessed with array 
> notation. They look like they should work with Array generic methods... 
> and they do... except they aren't reliable if you use numeric keys... 
> but they don't even give an error message.

They work the same way as a collection does when one of the items in a 
collection has a name that's numeric, e.g. document.images with an <img> 
element whose name="" attribute has the value 0.


> I would suggest one of:
> 1. When accessed as an array they are treated as a readonly array. i.e. MUST
> use getItem(), setItem() for numeric keys
> 2. Treat it as an associative array. Change the name of the length property.

Storage works exactly like a collection in this respect; the values from 0 
to length-1 override the names.

-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

Received on Thursday, 30 April 2009 06:12:44 UTC