On Thu, Feb 3, 2011 at 8:09 AM, Asen Bozhilov <asen.bozhi...@gmail.com>wrote:

> Juriy Zaytsev :
>
> > But is it a bug really? As far as I know, web storage spec (
> > http://dev.w3.org/html5/webstorage/) makes no clarification on whether
> > interface members are writable; or enumerable/configurable for that
> matter.
> > WebIDL spec, however, does mention something about interface members
> having
> > attributes { [[Writable]]: true, [[Configurable]]: false, [[Enumerable]]:
> > false } (http://dev.w3.org/2006/webapi/WebIDL/#es-interfaces).
>
> Thanks for the links and the info.
>
> > So it seems like writability of localStorage's getItem, setItem, clear,
> etc.
> > is actually in accordance to spec... which is probably not that great,
> since
> > it only makes for a more fragile implementation. The opposite argument
> could
> > be that [[writable]]: false would disallow "useful" cases of overwriting
> > localStorage methods, whatever those cases might be.
>
> The problem is that they are not actually localStorage's methods. They
> are inherited trough the prototype chain from `Storage' interface.
>
> localStorage.setItem('foo', 'bar');
>
> localStorage.getItem = null;
>
> localStorage.getItem('foo'); //TypeError
>
> localStorage.__proto__.getItem.call(localStorage, 'foo'); //bar
>
> The setter in this case shadows `getItem' in the prototype chain and
> here IMHO internal attributes wouldn't help a lot, while
> `localStorage' instance hasn't own property `getItem'.
> I think they have to specified the setter and getter of Storage instances.
>

So `setItem` sets keys on an object, shadowing interface members defined on
the [[Prototype]]... Providing getItem/setItem shortcuts in terms of ES
property access/assignment is always problematic, since objects are likely
to inherit from `Object` (or other interfaces) and so can't be used as
reliable "hash tables".

It's the same shoot-in-the-foot-convenience, as providing access to elements
via their id/name off of document (like IE did back in the days, and the
rest followed the suit, if I recall correctly). Ditto for form controls
exposed through the form.


>
> Another interesting part of `localStorage' are integer properties. For
> example getter with integer is mimic of `localStorage.key(n)';
>
> localStorage.setItem('foo', 'bar');
>
> localStorage[0]; //returns foo
>
> localStorage[21]; //throws an Error with message: Index or size is
> negative or greater than the allowed amount
>
> localStorage[-1];
>
> This is about the getter, the more interesting part is. Could set an
> item which key is integer digit?
>
> localStorage.setItem('10', 'bar');
>
> localStorage.hasOwnProperty('10'); //true
>
> localStorage[10]; //Error try to get the 10th key instead of item with key
> 10
>
> localStorage.getItem('10'); //bar
>
> Another example using regular setter instead of `setItem':
>
> localStorage[666] = 'bar'; //There is not any errors here
>
> localStorage.getItem(666); //bar
>
> localStorage[666]; //Error
>

I didn't know numeric keys have special behavior. Is this part of the spec?
That's one lovely interface...

[...]

-- 
kangax

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to