2010-11-12 75 views
1

刪除代碼塊。如果我把這個代碼和編譯(高級優化)谷歌關閉編譯推進:在編譯時

/**@constructor*/ 
function MyObject() { 
    this.test = 4 
    this.toString = function() {return 'test object'} 
} 
window['MyObject'] = MyObject 

我得到這個代碼

window.MyObject=function(){this.test=4;this.toString=function(){return"test object"}}; 

有什麼辦法,我可以使用Closure編譯器去掉toString函數?

回答

3

toString是隱式調用的,所以除非Closure編譯器可以證明MyObject的結果永遠不會被強制爲一個字符串,它必須保留它。

您可以隨時將其標記爲明確的調試代碼:

this.test = 4; 
if (goog.DEBUG) { 
    this.toString = function() { return "test object"; }; 
} 

然後在非調試版本,編譯

goog.DEBUG = false; 

http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.source.html這確實

/** 
* @define {boolean} DEBUG is provided as a convenience so that debugging code 
* that should not be included in a production js_binary can be easily stripped 
* by specifying --define goog.DEBUG=false to the JSCompiler. For example, most 
* toString() methods should be declared inside an "if (goog.DEBUG)" conditional 
* because they are generally used for debugging purposes and it is difficult 
* for the JSCompiler to statically determine whether they are used. 
*/ 
goog.DEBUG = true; 
+0

我試圖使用它,但是如何將其指定給http://closure-compiler.appspot.com/home中的編譯器?我試着設置goog.DEBUG-false;但它不起作用...... :( – edbras 2011-02-22 22:15:10

+0

@edbras,你可以使用-D標誌來關閉編譯器來設置它。從幫助:'--define(--D,-D)VAL:覆蓋值[= ],其中是@define變量的名稱,而是不包含單引號的布爾值,數字或單引號字符串。 ]被省略,該變量被標記爲真' – 2011-02-23 01:00:38

+0

我有同樣的問題。試圖使編譯器strip.log調用SIMPLE優化。對於我來說goog.DEBUG在聯機服務中不起作用。 – hellectronic 2012-08-21 16:15:44