gpt4 book ai didi

javascript - 谷歌闭包编译器高级 : remove code blocks at compile time

转载 作者:行者123 更新时间:2023-11-30 13:37:29 25 4
gpt4 key购买 nike

如果我使用这段代码并编译它(高级优化)

/**@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 函数吗?

最佳答案

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;

关于javascript - 谷歌闭包编译器高级 : remove code blocks at compile time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4167519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com