s.matushonok
s.matushonok

Reputation: 7585

JavaScript: The Good Parts Names/Strings railroad diagrams confusing

I started reading JavaScript: The Good Parts book and became confused at first pages (7 and 9 if be exact) by the railroad diagrams.

There are diagrams for the name and the string literal. (you can see the diagrams here)

As I know names in JavaScript can start with letters as well as with underscore or even '$' symbol. Following by the book's diagram it can starts only from letters.

For the string literal, imagine that you want to represent string like that: "\\".
Following by the diagram it's not possible(seems like after the "escaped character" the "any unicode character except " and \ and contol" is required.

Are those an errors in the book? Or I miss something?

Upvotes: 8

Views: 1118

Answers (3)

Šime Vidas
Šime Vidas

Reputation: 185933

The language described in the book "The Good Parts" is not JavaScript but a subset of it - you can call it "Crockford's JavaScript". Crockford introduces new syntax restrictions. One of those restrictions is that names cannot start with $ or _.

Upvotes: 8

Aadit M Shah
Aadit M Shah

Reputation: 74204

JavaScript literal names may start with letters, digits, underscores, or dollar signs. However, it's a convention to use only letters as the first character. http://javascript.crockford.com/code.html

The string literal may have any escaped character (e.g. "\"" or "\\") or any other character save the delimiter (" or ') and the backslash (\). The escaped character is not a single backslash. It's the representation of the whole escaped character. See the escaped character rail diagram in your own link.

Upvotes: 1

Ned Batchelder
Ned Batchelder

Reputation: 375634

It looks like the name railroad diagram definitely has problems. As you say, a name can start with underscore, but the diagram precludes it, and dollar signs seem to be completely missing.

You're wrong about the string literals though, since "\" is properly not allowed.

One for you, one for Crockford.

Upvotes: 1

Related Questions