As you know, JavaScript has all Java keywords reserved. Does anyone know why? JavaScript discourages using these Java keywords, but they appear to work fine when used as identifiers.
Reserved words (also called keywords) are defined with predefined meaning and syntax in the language. These keywords have to be used to develop programming instructions. Reserved words can't be used as identifiers for other programming elements like name of variable, function etc.
In JavaScript, the this keyword allows us to: Reuse functions in different execution contexts. It means, a function once defined can be invoked for different objects using the this keyword. Identifying the object in the current execution context when we invoke a method.
There are a total of 63 reserved words in JavaScript.
The story is that when they were developing JavaScript (originally called Oak I believe (apparently, I got the languages mixed up the previous statement about it's original name is incorrect.)), Netscape partnered with Sun to develop it. To entice the Java community, they wanted to make JavaScript like Java, so that Java developers would feel more comfortable with it, and that is the reason why they are so similar.
What you have to remember about designing a language is that you really only get one shot at defining keywords, without having a new version of the language break existing code. It's much easier to reserve a word at the beginning and not use it than try and reserve it later. This is especially true for things like JavaScript where the person developing the script has no control over which browser it's going to run in (this happens now i know, but it could be a lot worse). Could you imagine how madding it would be to find out that the next version of JavaScript which is adopted by the new browser suddenly won't run your application, because they reserved a new keyword?
Well, it's not actually Java keywords, as Javascript doesn't have anything to do with Java except borrowing the name.
The names were reserved in case they would be needed in future expansions of the language. From the ECMAScript Language Specification:
"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..."
Probably to avoid potential confusion that using these keywords as variable names could cause.
I mean, just imagine a Java developer trying to figure out a piece of JavaScript code with a variable or a function named public
.
Even when that is possible, I'd advise against it, for the sake of clarity.
Presumably the developers of JavaScript assumed that these words may be used for additional functionality in a future release of JavaScript. To prevent backwards-compatibility issues, these words were preemptively marked as reserved.
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