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.
The javascript implementation used by browsers has always been bending the rules. Part of the javascript DOM API is not even possible in pure javascript, for instance the dom innerHTML="something" that triggers an event. This was fixed in EcmaScript5, but you can't rely on the Browser Object Model being 100% legit javascript. AFAIK, as long as you don't put a foot into the DOM and the BOM, you can rely completly on the ecmascript standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With