简体   繁体   English

JavaScript - package 是保留关键字

[英]JavaScript - package is a reserved keyword

I am trying to minify a third-party JavaScript library using Google Closure Compiler, but it errors out at below line:我正在尝试使用 Google Closure Compiler 缩小第三方 JavaScript 库,但在以下行出错:

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.上面的name是函数内部的局部变量, inBlock是输入参数。 Nowhere in the function is package declared other than that error line.除了那个错误行之外,函数中没有任何地方声明package

I guess it may be due to package is a reserved keyword in JavaScript?我猜可能是因为package是 JavaScript 中的保留关键字? Any idea what package is in JavaScript and how to fix it?知道 JavaScript 中有什么包以及如何修复它吗?

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是 JavaScript 中的保留字(但仅限于严格模式,这将是代码在某些地方工作的原因)。

package is future-reserved, which means it's not used for anything, but you can't use it to name variables. package是未来保留的,这意味着它不用于任何事情,但你不能用它来命名变量。 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.对于那些想知道今天是否仍然如此的人 - package被添加到 ES-3 (1999) 中的未来保留列表中,并且一直保留到今天。 At the time of writing, we are at ES-11 (2020), where it is still unavailable.在撰写本文时,我们处于 ES-11(2020),它仍然不可用。

The relevant parts of the ES-11 2020 spec are: ES-11 2020 规范的相关部分是:

11.6.2 Note 2 : 11.6.2 注 2

enum is not currently used as a keyword in this specification. enum目前在本规范中不用作关键字。 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.类似地, implementsinterfacepackageprivateprotectedpublic是严格模式代码中的未来保留字。

and 12.1.1 SS: Early Errors :12.1.1 SS:早期错误

Identifier : IdentifierName but not ReservedWord Identifier : IdentifierName但不是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 ".如果此短语包含在严格模式代码中并且 IdentifierName 的 StringValue 为:“ implements ”、“ interface ”、“ let ”、“ package ”、“ private ”、“ protected ”、“ public ”、“ static “,或“ yield ”。

package is a keyword (from Java) reserved for possible later use in JavaScript. package是一个关键字(来自 Java),为以后可能在 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的名称,请inBlock.package括号表示法访问它:

inBlock['package']

According to MDN , package is in the "reserved for the future" category.根据MDNpackage属于“为未来保留”类别。 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.但这对您的package变量没有帮助。 Can you rename it?你能重命名吗?

"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). “package”是 ecmascript 3 中的保留字。ecmascript 5 减少了保留字集,使其可用于实现它的浏览器,并在 ecmascript 5“严格”模式下再次引入它(这将成为未来 emcascript 修订的基础) .

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. Ecmascript 5 还更改了对保留字的限制,特别是,您可以使用保留字作为属性名称(无论模式如何),但不能使用变量名称。

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.因此,如果您将 Closure Compiler 置于 EcmaScript 5 模式,您可以使用“inBlock.package”并且它不会抱怨,但是如果您尝试在较旧的 IE 版本(我相信是 8、7、6)中使用它将无法解析。 Most other browsers did not follow that part of the spec and are not affected.大多数其他浏览器不遵循规范的那部分,因此不受影响。

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

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