Google Closure Compiler advanced: remove blocks of code at compile time

If I take this code and compile it (advanced optimizations)

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

I get this code

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

Is there a way to remove the toString function using the Closure compiler?

+1
javascript google-closure-compiler
source share
1 answer

toString implicitly called, so if the Closure compiler cannot prove that the result of MyObject never coerced into the string that it should save.

You can always mark it as explicit debugging code:

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

then in your assembly without debugging, compile with

 goog.DEBUG = false; 

See http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.source.html which does

 /** * @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; 
+3
source share

All Articles