“delete”的返回值的真正含义是什么? [英] What is the true meaning of the returned value of `delete`?

查看:1360
本文介绍了“delete”的返回值的真正含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此MDN页面 delete 关键字


只有属性存在且不能删除时才返回false。

但是,我发现 delete 的情况, code>返回 true ,尽管该属性未被删除:

 删除窗口
删除警报
删除目录
删除控制台
删除2
删除空白
删除{} .x
...

实际上,几乎所有 window 通过在 about:blank 中运行以下脚本可以看出 delete

  for(a in window){if(delete window [a]){console.log(a);然而,大多数属性 window  



>

>实际上不会被删除。返回值 delete 的真正含义是什么?为什么它会返回 true 它没有删除的属性?



(注意:我会对参考文献感兴趣以解释 delete 的行为的Chromium代码。) 是一个主机对象,其语义由主机环境定义,例如浏览器。 delete 在应用于主机对象的属性时比应用于本机对象时要复杂。




$主机对象可以支持这些内部属性和任何依赖于实现的行为,只要它符合本文所述的特定主机对象限制。 b $ b

第11.4.1节 - 删除如果IsStrictReference(ref)是真的,抛出一个SyntaxError异常。
否则,返回true。

所以当一个主机对象不支持删除或修改一个属性时,它返回一个无法解析参考或假装被删除的参考。任何一种方法都会导致 true 以非严格模式返回。


According to this MDN page, the delete keyword

Returns false only if the property exists and cannot be deleted. It returns true in all other cases.

However, I see cases where delete returns true, despite the property not being deleted:

delete Window
delete alert
delete dir
delete console
delete 2
delete null
delete {}.x
...

In fact, almost all properties of window return true with delete, as can be seen by the running the following script in about:blank:

for(a in window) { if(delete window[a]) { console.log(a); } }

However, most properties of window do not actually get deleted. What is the true meaning of the returned value of delete? Why does it return true for properties it doesn't delete?

(Note: I would be interested in references to Chromium code explaining the behaviour of delete.)

解决方案

The window is a host object, one whose semantics are defined by the host environment, e.g. the browser. delete when applied to properties of host objects is more complicated than when applied to native objects.

Host objects may support these internal properties with any implementation-dependent behaviour as long as it is consistent with the specific host object restrictions stated in this document.

Section 11.4.1 - The delete operator says

If IsUnresolvableReference(ref) then,
  If IsStrictReference(ref) is true, throw a SyntaxError exception.
  Else, return true.

so when a host object doesn't support deletion or modification of a property, then it returns an unresolvable reference or a reference which pretends to be deleted. Either approach causes true to be returned in non-strict mode.

这篇关于“delete”的返回值的真正含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆