我如何告诉 Google 闭包编译器不要删除 var

How can I tell Google closure compiler to not remove a var

我有以下代码:

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

var l = window.location;
var s = 'hash';
l[s] = 'whatever i need now';

使用 google 闭包编译器(高级模式)编译,如下所示:

window.location.hash="whatever i need now";

但在这种情况下,我真的需要它在编译代码中继续使用 l[s]= ...

有没有办法告诉编译器继续使用 var 或忽略几行?

Compiler in action - demo

It's a small hack to get the hash function to work properly with junos pulse.

我很难相信黑客攻击是必要的,但是:

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

eval(
"var l = window.location;\n" +
"var s = 'hash';\n" +
"l[s] = 'whatever i need now';\n"
);

*哈克* *咳嗽* :-)

或者:

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

sessionStorage.x = "hash";
window.location[sessionStorage.x] = 'whatever i need now';