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)?
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.
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.
No state is not a reserved word.
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.
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']();
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With