gpt4 book ai didi

javascript - localStorage getters/setters for key "getItem": bug, 规范不一致或其他什么?

转载 作者:行者123 更新时间:2023-11-29 09:54:15 24 4
gpt4 key购买 nike

这段代码总结了我的问题:

localStorage.setItem('getItem', 4)
undefined
localStorage.getItem
function getItem() { [native code] }


localStorage['getItem'] = 'user'
"user"
localStorage.getItem
"user"
localStorage.getItem('getItem')
TypeError: Property 'getItem' of object #<Storage> is not a function

使用 setItem 方法或使用属性访问器改变 localStorage 的 react 方式。

这是一个错误吗?规范不一致?还有别的吗?

我觉得我应该在某个地方报告这个,但真的不知道去哪里。

最佳答案

Using the setItem method or using the property accessor changes the way localStorage reacts.

Is this a bug?

好像不是。来自spec :

When the setItem(), removeItem(), and clear() methods are called on a Storage object x that is associated with a local storage area, if the methods did something, then in every Document object whose Window object's localStorage attribute's Storage object is associated with the same storage area, other than x, a storage event must be fired, as described below.

localStorage 对象是一个 IDL attribute引用存储接口(interface)。 Storage 接口(interface)扩展了 Object.prototype,它引入了额外的属性,并且还定义了它自己的一组属性。

setItem 方法在存储列表中存储一个属性。当对象属性发生变化时,该对象也会触发一个事件,并在这些情况下更新存储区域。

因此 setItem 的预期行为是将键值对存储在存储列表中。 getItem 的预期行为是从存储列表中提取与键关联的值。

那么当我们尝试直接访问一个属性时,预期的行为是什么?这一行表示键值列表充当“支持的属性”:

The supported property names on a Storage object are the keys of each key/value pair currently present in the list associated with the object.

这是什么意思?这意味着 those properties are mapped to the getters and setters defined by the interface spec .对于存储,接口(interface)规范如下所示:

interface Storage {
readonly attribute unsigned long length;
DOMString? key(unsigned long index);
getter DOMString getItem(DOMString key);
setter creator void setItem(DOMString key, DOMString value);
deleter void removeItem(DOMString key);
void clear();
};

所以localStorage["x"]被映射到getItem("x")的值,返回存储列表中"x"的值, localStorage["x"] = y 将映射到 setItem("x",y)

但是,只有在对象上没有该名称的 native 属性时才会运行这些,除非 overridebuiltins属性在接口(interface)上设置(它不适用于存储)。

If the [OverrideBuiltins] extended attribute appears on an interface, it indicates that for a platform object implementing the interface, properties corresponding to all of the object’s supported property names will appear to be on the object, regardless of what other properties exist on the object or its prototype chain. This means that named properties will always shadow any properties that would otherwise appear on the object. This is in contrast to the usual behavior, which is for named properties to be exposed only if there is no property with the same name on the object itself or somewhere on its prototype chain.

因此对于属性,我们应该期望如果在对象上定义了一个函数,它将始终根据请求访问/设置它。否则它将检查存储列表并查看它是否有值并更改或返回它。

总结(TL;DR)

规范中的预期行为以及我们在您测试时发现的是 setItemgetItem 会将预先存在的属性/函数的值存储为键/值对,并访问它们,而不会覆盖现有值。当我们尝试直接通过 localStorage["getItem"] 访问或修改这些值时,我们将改为访问本地属性,因为“支持的属性”规范要求默认情况下不覆盖内置属性。

关于javascript - localStorage getters/setters for key "getItem": bug, 规范不一致或其他什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15592579/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com