简体   繁体   English

如何防止Closure Compiler在全局名称空间中创建特定的变量名?

[英]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)? 我熟悉使用字符串文字和导出以防止Closure重命名变量的概念,但是如何防止Closure使用由其他代码(我没写过)用作全局变量的变量名?

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. 当加载jQuery时,上面会被破坏。

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 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. 我认为output_wrapper选项用于解决这个问题,但要么我误解了它的目的,要么我误用了它。

TIA. TIA。

The answer is externs . 答案是外部

Externs define symbols in external code. Externs在外部代码中定义符号。 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. 在您的情况下,您可以包含jQuery的现有extern。 There is one for each major version in the project contrib folder . 项目contrib文件夹中的每个主要版本都有一个。

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

相关问题 如何在简单模式下告诉Google Closure Compiler可以压缩特定的函数名称? - How to tell Google Closure Compiler in simple mode that particular function name could be compressed? Google Closure编译器-如何为变量创建外部变量(变量名称不能像在Eval中一样更改) - Google Closure Compiler - How to create an Extern for a variable (variable name can't change as it is in an Eval) 闭包编译器重命名类名 - Closure compiler renames class name 如何使用javascript eval防止意外的全局名称空间污染? - How do I prevent accidental global name space pollution with javascript eval? 如何防止Closure Compiler重命名为“true”,“false”和“null” - How to prevent Closure Compiler from renaming “true”, “false” and “null” 如何防止Google Closure Compiler删除一行 - How to prevent Google Closure Compiler from removing a line 如何在JS中引用全局名称空间 - How to refer to Global name space in JS 如何将服务器名称设置为xml文件中javascript中的全局变量? - How to set the server name as a global variable in javascript from an xml file? Closure编译器更新字符串属性名称引用 - Closure Compiler update string property name references 由于名称属性,Closure Compiler重命名麻烦 - Closure Compiler renaming trouble due to name property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM