简体   繁体   中英

How to prevent Closure Compiler from creating a particular variable name in the global name space?

I'm familiar with the concept of using string literals and exporting to prevent Closure from renaming variables, but how do I prevent Closure from using a variable name that is used as a global variable by other code (which I did not write)?

Example, below was created for a member function of a closure:

  function $() {
  var a;
  if(1 > N) {
        return-1
  }
  a = Math.pow(1 + Q, F);
  return .....
  }

Above gets clobbered when jQuery is loaded.

I'm using the command line compiler and this is my command line:

java -jar compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --formatting=pretty_print --output_wrapper PGS --js common.v2.0.raw.js --module common_min:1 --js page_code.raw.js --module page_code_min:1:common_min

I thought the output_wrapper option is used to solve this problem, but either I'm misunderstanding its purpose or I'm misusing it.

TIA.

The answer is externs .

Externs define symbols in external code. They have two main purposes:

  • provide definitions and type information for external symbols so your code can call them without errors/warnings.
  • prevent the compiler from using symbol names defined outside of your code

In your case you can include an existing extern for jQuery. There is one for each major version in the project contrib folder .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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