Here's an excerpt from Chapter 2 Names:
A name cannot be one of these reserved words:
...
delete
...
Then later in that section:
Names are used for ... property names...
If that's true, then why does this work in my browser console:
> a = {};
> a.delete = 1;
> a.delete + 2 === 3
true
I'm using "Version 36.0.1985.143 m" of Google Chrome for my browser and Windows 8.1 Enterprise for my OS.
Does the author mean property names should not be a reserved word or am I missing something?
The delete operator deletes a property from an object: The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.
When applying the delete operator on a property accessor, the operator removes the corresponding property from the object: Try the demo. Initially, employee has 2 properties: name and position. But after applying the delete operator on the position property: delete employee.position, the property is removed from the object. Simple as that.
Table of contents 1 Good Parts Why JavaScript? Analyzing JavaScript A Simple Testing Ground 2 Grammar Whitespace Names Numbers Strings Statements Expressions Literals Functions 3 Objects Object Literals Retrieval Update Reference Prototype Reflection Enumeration Delete Global Abatement More items...
The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties.
When the book was written, ECMAScript 3 ruled the earth and that was a true statement.
However, modern browsers are running ECMAScript 5, which has lifted that requirement.
If you have IE, put yourself into IE6 or IE7 compatibility mode and it will fail.
BTW, even with ECMAScript 3, you can still write it as
a["delete"]
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