根据
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); } }
解决方法
该窗口是
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.