Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"JavaScript: The Good Parts" - Why can delete be used as a property name when the book implies it shouldn't

Tags:

javascript

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?

like image 397
Jesus is Lord Avatar asked Aug 16 '14 20:08

Jesus is Lord


People also ask

What happens when you delete a property in JavaScript?

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.

How to delete an object from a property accessor?

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.

What are the parts of JavaScript?

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...

What does the delete operator do in JavaScript?

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.


1 Answers

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"]
like image 145
Jeremy J Starcher Avatar answered Oct 01 '22 07:10

Jeremy J Starcher