Re: [Web Storage] A couple questions about the storage spec

On Tue, Aug 17, 2010 at 12:08 AM, Ian Hickson <ian@hixie.ch> wrote:

> On Thu, 8 Jul 2010, David John Burrowes wrote:
> >
> > I have a couple questions about the storage spec (I'm reading the June
> > 15th version at (http://dev.w3.org/html5/webstorage/).
> >
> > (1) The spec says: "The object's indices of the supported indexed
> > properties are the numbers in the range zero to one less than the number
> > of key/value pairs currently present in the list associated with the
> > object. If the list is empty, then there are no supported indexed
> > properties."
> >
> > As far as I can tell, this seems to say I should be able to say
> > something like:
> >       window.localStorage[3]
> > and get something back (not clear if the key or the value).  Am I right
> > in my interpretation of that paragraph?
>
> Yes.
>
>
> > I saw some discussion earlier about whether something like
> > localStorage[3] was meaningful, but I didn't find the resolution.  It
> > does seem undesirable/confusing to me.  And none of the browsers I've
> > tried this with do this.
>
> On Wed, 14 Jul 2010, Jeremy Orlow wrote:
> >
> > All the browsers I know of handle localStorage[3] as
> > localStorage.get/setItem('3', ...).  My impression is that this behavior
> > is pretty firmly rooted at this point.  It seems as though the spec may
> > need to change.
>
> Firefox seems to implement this correctly. It just seems to be a bug in
> Opera and Chrome. (I didn't test Safari or IE.)
>

Safari and IE act the same way as Opera and Chrome, so I would agree with
David's assertion that this behavior is fairly firmly rooted.  But you
disagree, Ian?  If so, let me just make sure I understand correctly: if you
do localStorage[x] and x is a number, then it should be an index into item
number x, and if x is anything else, it should be interpreted as
.get(x)/.set(x, ...) ?


> This is the kind of thing that will shake out when we have a test suite.
>
>
> On Thu, 8 Jul 2010, David John Burrowes wrote:
> >
> > (2) The spec also says:
> > "The names of the supported named properties on a Storage object are the
> > keys of each key/value pair currently present in the list associated
> > with the object."
> > I read that (possibly/probably wrongly) as saying I should be able to say
> >       window.localStorage.setItem("foo", "bar");
> >       myVariable = window.localStorage["foo"];
> > and now myVariable will have "bar".
>
> Yes.
>
>
> > If my reading is right (and it is the behavior I see in a couple
> browsers) this makes me very nervous, because I can do something like:
> >       window.localStorage.setItem("length", "a value we computer");
> >       window.localStorage.setItem("clear", "something that is
> transparent");
> > which of course allows:
> >       window.localStorage["length"];
> >       window.localStorage["clear"];
> > but in the browsers I've looked at, this (of course) also kinda messes up
> things like:
> >       for (index = 0; index < window.localStorage.length; index++) {
> >               // whatever
> >       }
> >       window.localStorage.clear();
> > since length is now not a number, and clear isn't a function.
>
> Actually length and clear don't get overrideen, because the interface is
> not defined as [OverrideBuiltins].


Wha?  This seems completely different than your answer in this thread:
http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2009-June/020349.html


>
> > Why is this a desirable feature?
>
> Code like this:
>
>   if (localStorage.visitedAlready) { ... }
>
> ...is simpler to read than code like:
>
>   if (localStorage.getItem('visitedAlready')) { ... }
>
>
> > (3) Real nitpicking here:
> > The IDL for the Storage interface says
> >   setter creator void setItem(in DOMString key, in any data);
> > but the text says
> >       The setItem(key, value) method
> > Note the name of the second parameter is different between these.
>
> Fixed IDL.
>
> --
> Ian Hickson               U+1047E                )\._.,--....,'``.    fL
> http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
> Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'
>

Received on Tuesday, 17 August 2010 11:40:20 UTC