Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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?

like image 285
iwan Avatar asked Dec 20 '11 00:12

iwan


People also ask

Which is not reserved keyword in JavaScript?

The NaN , Infinity , and undefined are not reserved keywords in JavaScript But you must avoid using them. They are properties of the global object and have special meanings.

Is array a reserved word in JavaScript?

Save this question. Show activity on this post. Some SPECIAL words such as Object , Array , Function , Method , Number etc are not belong to keywords in Javascrpt: Reserved Keywords in Javascript.

Is parent a reserved word in JavaScript?

'parent' is not a reserved word but a global object in the browser's execution environment. Whether or not you want to have a variable name that conflicts with that is your decision.


2 Answers

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

like image 177
Timothy Jones Avatar answered Sep 25 '22 17:09

Timothy Jones


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?

like image 24
nnnnnn Avatar answered Sep 24 '22 17:09

nnnnnn