delete
removes properties, it doesn't destroy the value associated with a property.
delete obj
is just trying (and failing) to remove the local variable obj
from the current activation record.
It fails because an activation record, though it is an object, cannot have properties removed in this way.
One way to tell when a delete
fails is to look at its return value, which is a boolean where true indicates that there is no such property/binding after the delete
completes:
if (delete obj) {
alert('the obj property was deleted');
} else {
alert('delete did nothing');
}
Put that inside (function () { var obj; ... })()
and it will tell you that it did nothing.
Put that inside with ({ obj: null }) { ... }
and it will tell you that it worked.
In strict mode, any delete
that would return false should instead raise an exception, and delete
s that can never work should result in a parse-time error.