Reputation: 2597
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)?
Upvotes: 27
Views: 15239
Reputation: 182
Now it's used in ECMAScript® 2015 Language Specification:
class Foo {
constructor(bar) {
this.bar = bar;
}
}
new Foo(10).bar; // 10
Upvotes: 7
Reputation: 179226
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']();
Upvotes: 28
Reputation: 890
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
Upvotes: 2
Reputation: 262794
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
.
Upvotes: 9
Reputation: 27415
class
is reserved as a future keyword by the ECMAScript specification
Upvotes: 4