27

I am planning to implement class inherit interface using JavaScript. class is the perfect word as the name of the constructor.

class = function (classname) {}; // create a class with classname
classA = new class("classA"); // create a class named classA

Unfortunately, class is a reserved word in JavaScript.

Why does JavaScript reserve the word class (since it never uses it)?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
wukong
  • 2,430
  • 2
  • 26
  • 33
  • 1
    Possible duplicate? http://stackoverflow.com/questions/1728984/class-keyword-in-javascript – RoccoC5 Sep 23 '11 at 05:28
  • When "class" doesn't work, you can use nonsensical alternatives like "klass" or "clazz". It's a matter of sticking to a convention. – Ates Goral Sep 23 '11 at 05:31

5 Answers5

28

It's reserved to future-proof ECMAScript

The following words are used as keywords in proposed extensions and are therefore reserved to allow for the possibility of future adoption of those extensions.

Don't fret though, if you're using best-practices in your JavaScripts, you're placing all accessible functions/variables/constructors in a namespace, which will allow you to use whatever name you'd like:

foo = {};
foo['class'] = function(){...code...};
var myClass = new foo['class']();
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 2
    Note that `Class` is not reserved (capital `C`), and it could be used without problems as the identifier of a variable or function declaration (in fact, is recommended to capitalize the names of constructors). Now, `foo.RESERVEDWORD` will not work on ES3 implementations, since keywords (reserved and future-reserved words) are not allowed in the grammar of the dot property accessor, nor in the object literal syntax (BTW, that's why in JSON format, keys must be quoted...). This changes on ES5, now is possible to access any valid identifier name, even if it's reserved, e.g. `({if:1}).if == 1;` – Christian C. Salvadó Sep 23 '11 at 06:11
  • 1
    Here's a direct link to "*7.6.1.2 Future Reserved Words*" in the HTML version of the spec: http://www.ecma-international.org/ecma-262/5.1/#sec-7.6.1.2 – apsillers Aug 08 '13 at 19:28
9

Languages evolve. It is prudent to reserve a few keywords that might come in use later. According to the ECMA standard, class is a Future Reserved Word.

If you did not reserve them, introducing new keywords could conflict with existing code (which already might be using the word for other things). Happened with Java and assert.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    Very true about the evolution! We can now use class keyword in javascript code starting ECMA6 (aka ECMA 2015) as mentioned [here](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes). – RBT May 18 '17 at 01:38
7

Now it's used in ECMAScript® 2015 Language Specification:

class Foo {
    constructor(bar) {
        this.bar = bar;
    }
}
new Foo(10).bar; // 10
halfzebra
  • 6,771
  • 4
  • 32
  • 47
username.ak
  • 182
  • 1
  • 2
  • 16
4

class is reserved as a future keyword by the ECMAScript specification

Reserved Words - MDN

MikeM
  • 27,227
  • 4
  • 64
  • 80
2

It is there so that support can be added in future without breaking existing programs. Please take a look at the following post.

You can also look at

https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words

Community
  • 1
  • 1
Sivakumar
  • 890
  • 1
  • 14
  • 27