17

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?

Jee Mok
  • 6,157
  • 8
  • 47
  • 80
iwan
  • 7,269
  • 18
  • 48
  • 66

4 Answers4

23

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

Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
  • You don't really need to use the bracket syntax, though. [jsFiddle](http://jsfiddle.net/4Lpyw/) @nnnnnn – Ry- Dec 20 '11 at 00:20
  • That might work in some interpreters, but it's illegal (JavaScript: The Good Parts, page 103 - can look up the standard if you like). – Timothy Jones Dec 20 '11 at 00:22
  • Oh, didn't know that. Well, it works in every browser I'm able to test at least :) – Ry- Dec 20 '11 at 00:23
  • @minitech - That's why I said that you can "safely use" the bracket syntax: it will work everywhere, while the dot syntax might work in most current browsers but don't count on it for the future...just another of those little odd "features"... – nnnnnn Dec 20 '11 at 00:53
2

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']
Ry-
  • 218,210
  • 55
  • 464
  • 476
2

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?

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
2

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

John
  • 5,443
  • 15
  • 21