2016-01-07 28 views
0

我有以下代碼:我怎麼能告訴谷歌關閉編譯器不刪除變種

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

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

而它與谷歌關閉編譯器(高級模式)這樣的編譯:

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

但在這種情況下,我真的需要它在編譯後的代碼中繼續使用l[s]= ...

有沒有辦法告訴編譯器繼續使用var或忽略幾行?

Compiler in action - demo

+0

請勿使用高級模式?它是實驗性的,可能會損害你的代碼。簡單模式通常足夠有效。 –

+0

我應該在問題中說過這個,但這不是一個選項。 – yeouuu

+4

我很好奇你爲什麼需要它。如果它在其他地方使用,應該保留,不是嗎? – Marc

回答

3

這是一個小黑客獲得哈希函數來的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'; 
+0

哇,這是骯髒的,因爲它可以是,恭喜使用eval :) –

+0

@JeremyThille:當黑客,認爲'eval',我說。 ;-) –

+0

@yeouuu:是的。它保持變量,這就是你說的你需要的。 –

相關問題