我使用谷歌闭包编译器编译了这个例子的JavaScript代码。
并且,我从中得到了这个输出。
当我使用
$(document).on('click', function () {
console.log('Hello');
});
并且,我从中得到了这个输出。
$(document).a("click",function(){console.log("Hello")});
在这里,on
被替换为 a
。因此,当我在浏览器上运行时,我遇到了错误。
我使用以下命令行来编译脚本。
java -jar closure-compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --js script.js
当我使用
--process_jquery_primitives
参数时,我遇到了这个错误。java.lang.RuntimeException: com.google.javascript.jscomp.CompilerOptionsPreprocessor$InvalidOptionsException: The jQuery pass and the Closure pass cannot both be enabled.
at com.google.javascript.jscomp.CompilerExecutor.runInCompilerThread(CompilerExecutor.java:126)
at com.google.javascript.jscomp.Compiler.runInCompilerThread(Compiler.java:740)
at com.google.javascript.jscomp.Compiler.compile(Compiler.java:710)
at com.google.javascript.jscomp.Compiler.compile(Compiler.java:680)
at com.google.javascript.jscomp.AbstractCommandLineRunner.doRun(AbstractCommandLineRunner.java:1080)
at com.google.javascript.jscomp.AbstractCommandLineRunner.run(AbstractCommandLineRunner.java:492)
at com.google.javascript.jscomp.CommandLineRunner.main(CommandLineRunner.java:1866)
Caused by: com.google.javascript.jscomp.CompilerOptionsPreprocessor$InvalidOptionsException: The jQuery pass and the Closure pass cannot both be enabled.
at com.google.javascript.jscomp.CompilerOptionsPreprocessor.preprocess(CompilerOptionsPreprocessor.java:74)
at com.google.javascript.jscomp.Compiler.compileInternal(Compiler.java:746)
at com.google.javascript.jscomp.Compiler.access$000(Compiler.java:84)
at com.google.javascript.jscomp.Compiler$2.call(Compiler.java:713)
at com.google.javascript.jscomp.Compiler$2.call(Compiler.java:710)
at com.google.javascript.jscomp.CompilerExecutor$2.call(CompilerExecutor.java:91)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
注意:我正在使用最新的闭包编译器(closure compiler)。
ADVANCED
模式编译。请参阅http://closuretools.blogspot.com/2012/09/which-compilation-level-is-right-for-me.html。 - Chad Killingsworth