Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is class a reserved word in JavaScript?

Tags:

javascript

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)?

like image 846
wukong Avatar asked Sep 23 '11 05:09

wukong


People also ask

What is reserved word in JavaScript?

13 / 31 Blog from JavaScript. In any programming language, a reserved word or a reserved identifier is a word that cannot be used as an identifier, such as the name of a variable, function, or label. Thus, it is reserved and cannot be used for defining any of these.

Why keywords are called reserved words?

Some use the terms "keyword" and "reserved word" interchangeably, while others distinguish usage, say by using "keyword" to mean a word that is special only in certain contexts but "reserved word" to mean a special word that cannot be used as a user-defined name.

Is state a reserved word in JavaScript?

No state is not a reserved word.

Is callback a reserved word in JavaScript?

For your top function, callback is the name of the third argument; it expects this to be a function, and it is provided when the method is called. It's not a language keyword - if you did a "find/replace all" of the word "callback" with "batmanvsuperman", it would still work.


2 Answers

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'](); 
like image 88
zzzzBov Avatar answered Oct 17 '22 04:10

zzzzBov


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.

like image 38
Thilo Avatar answered Oct 17 '22 05:10

Thilo