What happens in JavaScript if I have a variable, say:
var exampleObject = {one:'foo',two:'bar'};
and then I delete a property that doesn't exist, a la:
delete exampleObject.seven;
Is there a standard course of action that takes place everywhere (nothing, error message, script crashes, etc.), or is this dependent on some kind of implementation (browser engine, etc.)?
What happens in JavaScript if I have a variable, say:
var exampleObject = {one:'foo',two:'bar'};
and then I delete a property that doesn't exist, a la:
delete exampleObject.seven;
Is there a standard course of action that takes place everywhere (nothing, error message, script crashes, etc.), or is this dependent on some kind of implementation (browser engine, etc.)?
Nothing happens.
Assuming, x = {}
, Type(x.y)
is not a Reference Specifcation Type (there cannot be a "reference" to a property that does not exist). According to 11.4.1 The delete Operator, this satisfies the rule:
- Let ref be the result of evaluating UnaryExpression.
- If Type(ref) is not Reference, return true.
- ...
This behavior (of "no action") has existed for a long time - any environment that behaves differently is non-compliant. From the 3rd Edition ECMAScript Specification:
When the [[Delete]] method of O is called with property name P, the following steps are taken:
- If O doesn’t have a property with name P, return true.
- ..
If exampleObject is an object, the return value from delete is true, even if the property does not exist.