javascript – “delete”的返回值的真正含义是什么?

前端之家收集整理的这篇文章主要介绍了javascript – “delete”的返回值的真正含义是什么?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
根据 this MDN page,删除关键字

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

但是,尽管没有删除属性,我仍然看到delete的值为true的情况:

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

其实几乎所有属性的窗口都返回true,删除,可以看出运行下面的脚本关于:blank:

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

但是,窗口的大多数属性实际上并没有被删除.删除的返回值的真正含义是什么?为什么它不会删除属性返回true?

(注意:我将有兴趣参考Chromium代码解释删除的行为.)

解决方法

该窗口是 host object,其语义由主机环境定义,例如浏览器.当应用于主机对象的属性时,删除比应用于本机对象时更复杂.

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

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

所以当一个主机对象不支持删除修改一个属性时,它返回一个不可解析的引用或一个假装被删除的引用.任何一种方法都将以非严格模式返回true.

原文链接:https://www.f2er.com/js/154381.html

猜你在找的JavaScript相关文章