JavaScript - package 是保留关键字

发布于 2024-12-22 03:59:48 字数 395 浏览 4 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

若能看破又如何 2024-12-29 03:59:48

你是对的,package 是 JavaScript 中的保留字(但仅在严格模式下,这就是代码在某些地方可以工作的原因)。

package 是未来保留的,这意味着它不用于任何用途,但您不能使用它来命名变量。但是(如果确实必须的话),您可以使用它来命名对象中的键,如下所示:

inBlock['package'] = name;  // this is ok

只要您使用字符串。你不能这样做:

inBlock.package = name;  // this is not ok

我想说你最好将其命名为其他名称。


对于那些想知道这在今天是否仍然成立的人来说 - package 被添加到 ES-3 (1999) 中的未来保留列表中,并且一直保留到今天。在撰写本文时,我们正处于 ES-11 (2020) 阶段,该功能仍不可用。

ES-11 2020 规范的相关部分是:

11.6.2 注 2

enum 目前在本规范中不用作关键字。它是未来的保留字,留作未来语言扩展中的关键字使用。

类似地,实现接口私有受保护、和 public 是严格模式代码中未来的保留字。

12.1.1 SS:早期错误:

标识符IdentifierName,但不是ReservedWord

如果此短语包含在严格模式代码中,并且 IdentifierName 的 StringValue 为:“implements”、“interface”、“,则这是语法错误让”,“”,“私有”,“受保护”,“公共” 、“静态”,或“产量”。

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:

inBlock['package'] = name;  // this is ok

As long as you use a string. You can't do this:

inBlock.package = name;  // this is not ok

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:

enum is not currently used as a keyword in this specification. It is a future reserved word, set aside for use as a keyword in future language extensions.

Similarly, implements, interface, package, private, protected, and public are future reserved words in strict mode code.

and 12.1.1 SS: Early Errors:

Identifier: IdentifierName but not ReservedWord

It is a Syntax Error if this phrase is contained in strict mode code and the StringValue of IdentifierName is: "implements", "interface", "let", "package", "private", "protected", "public", "static", or "yield".

我只土不豪 2024-12-29 03:59:48

根据 MDNpackage 位于“为未来保留”类别。根据您使用的浏览器版本以及您的代码是否处于严格模式,您可能能够也可能无法使用这些单词作为标识符。换句话说,为了安全起见,您应该避开它们。

如果您使用以下语法,您可以安全地使用保留字作为属性名称:

inBlock["package"] = something;

但这对您的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:

inBlock["package"] = something;

But that doesn't help you with your package variable. Can you rename it?

仅此而已 2024-12-29 03:59:48

package 是一个关键字(来自 Java),保留供以后在 JavaScript 中使用。解决方案是什么?将您的变量命名为其他名称:)

如果您无法更改 inBlock.package 的名称,请使用方括号表示法访问它:

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:

inBlock['package']
西瓜 2024-12-29 03:59:48

“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文