简体   繁体   English

尝试在严格模式下删除数据集属性时,在Safari上“无法删除属性”

[英]'Unable to delete property' on Safari when trying to delete dataset attribute in strict mode

Consider following code: 考虑以下代码:

(function () {
  'use strict';
  delete document.body.dataset.state;
})();

where body dataset is empty. body dataset为空的位置。 Safari treats all DOMStringMap values — well, I guess so — as ReadOnly , and their enumerable , configurable and writable descriptor values are all set to false . Safari将所有DOMStringMap值都视为ReadOnly ,并将其enumerableenumerable configurablewritable描述符值都设置为false This causes the TypeError: Unable to delete property to appear in the mentioned above example. 这会导致TypeError: Unable to delete property以显示在上面提到的示例中。

But in Chrome dataset property descriptor values are set to true (can be checked with Object.getOwnPropertyDescriptor() ), and deleting inexistent attribute does not throw the error. 但是在Chrome数据集中,属性描述符值设置为true (可以使用Object.getOwnPropertyDescriptor()进行检查),并且删除不存在的属性不会抛出错误。

So, what behavior is correct? 那么,什么行为是正确的? The spec says about readonly dataset , but writable DOMStringMap , so I assume dataset properties must be deletable. 该规范说明了readonly数据集 ,但可写DOMStringMap ,所以我假设数据集属性必须是可删除的。 Am I missing something? 我错过了什么吗?

Its probably because of strict mode. 它可能是因为严格的模式。

Third, strict mode makes attempts to delete undeletable properties throw (where before the attempt would simply have no effect): 第三,严格模式尝试删除不可删除的属性throw(在尝试之前根本没有效果):

'use strict';
delete Object.prototype; // throws a TypeError

You may want to take a look at this documentation 您可能需要查看此文档

Also you can try set object value to undefined, its a little hairy but works.. 你也可以尝试将对象值设置为undefined,它有点毛茸茸但是有效..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM