JavaScript - a package is a reserved keyword

I am trying to minimize a third-party JavaScript library using the Google Closure Compiler, but it throws an error in the line below:

inBlock.package = package = name 

Error

ERROR - Parse error. missing name after. Operator **

name above is a local variable inside the function, and inBlock is an input argument. Nowhere in the function is a package declared except for this error line.

I think this could be due to the fact that package is a reserved keyword in JavaScript? Any idea what the package is in JavaScript and how to fix it?

+10
source share
4 answers

You are right, package is a reserved word in JavaScript (but only in strict mode, and that is why the code works in some places).

package reserved for the future, which means that it is not used for anything, but you cannot use it to refer to variables. However (if you really should), you can use it to indicate keys in such objects:

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

While you use the string. You cannot do this:

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

I would say that you better call it something else.

+14
source

According to MDN , package is in the “reserved for the future” category. Depending on which version of the browser you are using and whether your code is in strict mode, you may or may not use these words as identifiers. In other words, you must avoid them in order to be safe.

You can safely use reserved words as property names if you use this syntax:

 inBlock["package"] = something; 

But that will not help you with your package variable. Can you rename it?

+2
source

"package" is a reserved word in ecmascript 3. ecmascript 5 reduced the reserved word by making it available to the browser that implemented it and presented it again in strict mode of ecmascript 5 (which should be the basis of future emcascript).

The escape script 5 also changed the restrictions on reserved words, in particular, you can use reserved words as property names (regardless of mode), but not variable names.

As a result, if you put the Closure Compiler in EcmaScript 5 mode, you can use "inBlock.package" and it will not complain, but if you try to use it in older versions of IE (I believe 8,7,6), it doesn’t will be able to make out. Most other browsers did not follow this part of the specification and were not affected.

+2
source

package is a keyword (from Java) reserved for later use in JavaScript. Decision? Call your variable something else :)

If you cannot change the name inBlock.package , use it instead of the notation:

 inBlock['package'] 
+1
source

Source: https://habr.com/ru/post/904124/


All Articles