简体   繁体   English

`java`是JavaScript中的保留关键字吗?

[英]Is `java` a reserved keyword in JavaScript?

I noticed that several JavaScript IDEs (eg Sublime) show the word java with a syntax highlighting like it is a reserved keyword. 我注意到一些JavaScript IDE(例如Sublime)显示了带有语法突出显示的单词java ,就像它是一个保留关键字一样。

Also older versions of Firefox have problems with a function called java. 旧版本的Firefox也存在一个名为java的函数的问题。

function java() {
    alert("This function can not be called");
}

Is this actually a keyword? 这实际上是关键字吗? If yes what is it for? 如果是的话是什么?

Well, it's not actually Java keywords, as Javascript doesn't have anything to do with Java except borrowing the name. 好吧,它实际上不是Java关键字,因为除了借用名称之外,Javascript与Java没有任何关系。

The names were reserved in case they would be needed in future expansions of the language. 如果在将来扩展该语言时需要这些名称,则保留这些名称。 From the ECMAScript Language Specification : 来自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...

http://www.javascripter.net/faq/reserved.htm http://www.javascripter.net/faq/reserved.htm

Ultimately the answer is it is not a reserved keyword , you should be safe in using java as a variable name. 最终答案是它不是保留关键字 ,使用java作为变量名称应该是安全的。

But as noted on http://www.javascripter.net/faq/reserved.htm it states that java is one of the words that should have been reserved words . 但正如http://www.javascripter.net/faq/reserved.htm所述,它指出java应该保留字的单词之一 And I believe the reasoning he is referring too is for Java Applet integration. 我相信他所指的推理也适用于Java Applet集成。

It is hard to find references of this on the web because the technology is largely deprecated. 很难在网上找到这方面的参考资料,因为该技术在很大程度上已被弃用。 It appears that up until Firefox 16 Mozilla included Globals for Packages , java , and netscape (See 2.1.5 Deprecated Functionality: the Global Packages, java and netscape Keywords ) 看起来直到Firefox 16 Mozilla包括Globals for Packagesjavanetscape (参见2.1.5不推荐的功能:全局包,java和netscape关键字

The only bits of this I could find are referenced in Mozilla's LiveConnect Documentation where you can see in Java in Firefox Extensions First the note 我可以找到的这一部分在Mozilla的LiveConnect文档中引用,您可以在Firefox中看到Firefox Extensions中的注释

Note: The global java object has been removed in Gecko 16.0, so this page is out of date 注意: Gecko 16.0中已删除全局java对象,因此该页面已过期

And then the sample code block: 然后是示例代码块:

var aJavaList = new java.util.LinkedList();

So basically before Mozilla had deprecated the global java object (and possibly applets were on the page?), this could have been a problem. 所以基本上在Mozilla弃用全局java对象(可能还有applet在页面上?)之前,这可能是一个问题。

According to this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words 根据这个: https//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words

No, it is not a reserved word. 不,这不是一个保留字。

Taking and running your function in a browser console confirms this. 在浏览器控制台中执行和运行您的功能可以确认这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM