admin 管理员组文章数量: 1086019
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.
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.
Share Improve this question edited Mar 21, 2011 at 15:22 Yi Jiang 50.2k16 gold badges138 silver badges136 bronze badges asked Jan 31, 2011 at 19:42 Tom TuckerTom Tucker 11.9k23 gold badges95 silver badges131 bronze badges 4- I believe it was part of the ECMAScript specification to not allow use of those keywords. – jrn.ak Commented Jan 31, 2011 at 19:48
- 2 Please note that the only people who really know the answer are original authors of JavaScript. All the rest of us can do here is speculate. – Goran Jovic Commented Jan 31, 2011 at 19:49
- 1 JavaScript pre-dates the existence of the ECMAScript spec. Were these reserved words present before then? (I don't know...) – Jason LeBrun Commented Jan 31, 2011 at 19:49
- 1 Some history: Netscape and Sun announce JavaScript - web.archive/web/20070916144913/http://wpscape./… – jasssonpet Commented Jan 31, 2011 at 21:22
4 Answers
Reset to default 4The 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 munity, they wanted to make JavaScript like Java, so that Java developers would feel more fortable 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-patibility issues, these words were preemptively marked as reserved.
本文标签: historyWhy JavaScript reserves Java keywordsStack Overflow
版权声明:本文标题:history - Why JavaScript reserves Java keywords? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744040198a2523110.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论