1

Somebody already asked a very similar question years ago - why is class a reserved word in JavaScript?. The answer was, so that you could add the class keyword, which we have now.

But I'd like to take that question one step further. Why does having a class keyword make it neccessary to forbid using class as an identifier? Is there anything in JS grammar that would make it ambiguous? Or is it so JS can be parsed with a certain kind of parser? I don't see that something like this would be impossible (although silly of course):

class class {
    // ...
}

let x = new class();

Please don't answer "because the spec says so" or "a keyword is forbidden as an identifier by definition". Why does the spec have to be this way? Or is it an arbitrary or aestetic descision?

This question is not completely idle curiosity. The fact that class is a forbidden identifier comes up in the debate whether React should use class or className in JSX, for example.

jdm
  • 9,470
  • 12
  • 58
  • 110
  • Because it's common reserved keyword? Because it keeps semantic? Because anywhay you need to name it somehow and class is the best name? IDK. – Drag13 Sep 01 '18 at 11:37
  • Yeah but why can't I write `let class="bourgeoisie";`? I understand why you would want to forbid redefining `true` or `null`, but is there any parser ambiguity that requires the JS language design to forbid a variable called `class`? – jdm Sep 01 '18 at 11:40
  • 1
    Think about the readability of the source code if you allowed that. Perhaps it's easy for you to understand what's going on because you wrote it and know exactly why you used it as an identifier. But for others that have read your code , they now have to start tracking this confusing identifier that also has another semantic. A good exercise might be taking a function of decent size and replace 1 of the main variables with this hypothetical class identifier and giving it to your colleagues to read it. And see their priceless reaction :) – Francis Sep 01 '18 at 11:51
  • Sure, I agree, it would probably be confusing. I'm just curious if it is gramatically neccessary to forbid this (because it would make the parser / syntax highlighter harder or impossible to write), or if the concern is "only" ergonomics. – jdm Sep 01 '18 at 11:57
  • 1
    Look at `async`, `await` and `yield` which are no keywords in sloppy mode and can be used as identifiers. The grammar (and parser) are considerably more difficult. – Bergi Sep 01 '18 at 12:43

1 Answers1

2

If class could be used as an identifier like this:

 class class 
 { // a class is followed by the classes body
   constructor() { /*...*/ }
 }

 const el = new class
 { // if class is not reserved, this is just a regular block
   alert("test");
 }

Then the new class part cannot be distinguished from a class expression:

 const el = new class
 {
   // block or class body?!
   constructor() { /*???*/ }
 }
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • But one is followed by a semicolon the other by braces, so we can distinguish them? – Bergi Sep 02 '18 at 12:02
  • @bergi if we argue like this, `var` doesnt have to be a reserved keyword too ... `var var = 1;` but then `var = 1` wouldnt be a syntax error and no one could write proper js because it would fail all the time without proper errors. – Jonas Wilms Sep 02 '18 at 12:06
  • @bergi and actually if we mix in the block statement, we got exactly the same code. – Jonas Wilms Sep 02 '18 at 12:10
  • 1
    Yes,`var` doesn't necessarily have to be a reserved keyword - and in fact, `let` is not outside of strict mode so we can write things like `var let = 1; let = 2;`. But indeed, proper error messages are a very good reason to make them keywords – Bergi Sep 02 '18 at 12:10