Closed Bug 1453710 Opened 7 years ago Closed 7 years ago

Object.keys(localStorage) misses keys named "key" (same with sessionStorage)

Categories

(Core :: DOM: Core & HTML, defect, P2)

defect

Tracking

()

RESOLVED INVALID

People

(Reporter: miker, Unassigned)

References

Details

STR: localStorage.setItem("key", "value1") localStorage.setItem("key2", "value2") Object.keys(localStorage) // Only lists key2 Same issue with sessionStorage but other objects are okay. Of course, localStorage.getItem("key") returns "value1".
Has Regression Range: --- → no
Has STR: --- → yes
This is because "key" is a method on the prototype. localStorage.setItem("setItem", "value3") would cause a similar issue.
Status: NEW → RESOLVED
Closed: 7 years ago
Resolution: --- → INVALID
localStorage.setItem("clear", "value") localStorage.setItem("constructor", "value") Object.keys(localStorage) // There are neither "clear" nor "constructor" here. Same to all the properties that are on the prototype chain of localStorage.
Are there any specification about this behavior? IMO, the keys in Storage should be enumerable properties of it no matter whether the key can be found in prototype chain, and should be listed in Object.keys().
I think the following example helps to understand this https://heycam.github.io/webidl/#example-9317c76c https://heycam.github.io/webidl/#OverrideBuiltins https://html.spec.whatwg.org/multipage/webstorage.html#the-storage-interface But I don't know why OverrideBuiltins isn't use here. File a spec bug if you want to change the behavior? (though, I guess at least localStorage is somewhat deprecated, so not sure why that would need to be changed.)
https://github.com/whatwg/html/issues/183 It is discussed before and this behavior conforms to the spec. I'll do some cross-browser test and reopen the issue if majority of browsers threats localStorage as [OverrideBuiltins].
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.