JavaScript - package 是保留关键字
我正在尝试使用 Google Closure Compiler 缩小第三方 JavaScript 库,但在下面一行出现错误:
inBlock.package = package = name
错误是
错误 - 解析错误。后面缺少名字。运算符**
name
是函数内的局部变量,inBlock
是输入参数。除了该错误行之外,函数中没有任何 package
声明。
我猜这可能是由于 package
是 JavaScript 中的保留关键字? 知道 JavaScript 中有什么包以及如何修复它吗?
I am trying to minify a third-party JavaScript library using Google Closure Compiler, but it errors out at below line:
inBlock.package = package = name
The error is
ERROR - Parse error. missing name after . operator**
name
above is a local variable inside a function and inBlock
is an input argument. Nowhere in the function is package
declared other than that error line.
I guess it may be due to package
is a reserved keyword in JavaScript?
Any idea what package is in JavaScript and how to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你是对的,
package
是 JavaScript 中的保留字(但仅在严格模式下,这就是代码在某些地方可以工作的原因)。package
是未来保留的,这意味着它不用于任何用途,但您不能使用它来命名变量。但是(如果确实必须的话),您可以使用它来命名对象中的键,如下所示:只要您使用字符串。你不能这样做:
我想说你最好将其命名为其他名称。
对于那些想知道这在今天是否仍然成立的人来说 -
package
被添加到 ES-3 (1999) 中的未来保留列表中,并且一直保留到今天。在撰写本文时,我们正处于 ES-11 (2020) 阶段,该功能仍不可用。ES-11 2020 规范的相关部分是:
11.6.2 注 2:
和 12.1.1 SS:早期错误:
You're right,
package
is a reserved word in JavaScript (but only in strict mode, which will be why the code works in some places).package
is future-reserved, which means it's not used for anything, but you can't use it to name variables. However (if you really must), you can use it to name keys in objects like this:As long as you use a string. You can't do this:
I would say you're better off naming it something else.
For those wondering if this is still true today -
package
was added to the future-reserved list in ES-3 (1999), and has remained there until today. At the time of writing, we are at ES-11 (2020), where it is still unavailable.The relevant parts of the ES-11 2020 spec are:
11.6.2 Note 2:
and 12.1.1 SS: Early Errors:
根据 MDN,
package
位于“为未来保留”类别。根据您使用的浏览器版本以及您的代码是否处于严格模式,您可能能够也可能无法使用这些单词作为标识符。换句话说,为了安全起见,您应该避开它们。如果您使用以下语法,您可以安全地使用保留字作为属性名称:
但这对您的
package
变量没有帮助。你能重命名吗?According to MDN,
package
is in the "reserved for the future" category. Depending on which version of which browser you are using and whether your code is in strict mode you may or may not be able to use those words as identifiers. In other words, you should avoid them to be safe.You can safely use reserved words as property names if you use this syntax:
But that doesn't help you with your
package
variable. Can you rename it?package
是一个关键字(来自 Java),保留供以后在 JavaScript 中使用。解决方案是什么?将您的变量命名为其他名称:)如果您无法更改
inBlock.package
的名称,请使用方括号表示法访问它:package
is a keyword (from Java) reserved for possible later use in JavaScript. The solution? Name your variable something else :)If you can't change the name of
inBlock.package
, access it using the bracket notation instead:“package”是 ecmascript 3 中的保留字。ecmascript 5 减少了保留字集,使其可供实现它的浏览器使用,并在 ecmascript 5“严格”模式中再次引入它(这将成为未来 emcascript 修订版的基础) 。
Ecmascript 5 还更改了对保留字的限制,具体来说,您可以使用保留字作为属性名称(无论模式如何),但不能使用变量名称。
因此,如果你将 Closure Compiler 置于 EcmaScript 5 模式,你可以使用“inBlock.package”并且它不会抱怨,但如果你使用尝试在较旧的 IE 版本(我相信是 8、7、6)中使用它将无法解析。大多数其他浏览器不遵循规范的该部分,因此不受影响。
"package" is a reserved word in ecmascript 3. ecmascript 5 reduced the reserved word set making this availables to browser that implemented it, and introduced it again in ecmascript 5 "strict" mode (which is to be the basis of future emcascript revisions).
Ecmascript 5 also changed the restrictions placed on reserved words, specifically, you can use reserved words as property names (regardless of mode) but not variable names.
As a result, if you put Closure Compiler into EcmaScript 5 mode you can use "inBlock.package" and it won't complain, but if you use try to use it in older IE versions (8,7,6 I believe) it will fail to parse. Most other browsers did not follow that part of the spec and are not affected.