闭包编译器不会虚拟化匿名包装器中包含的原型函数
我正在尝试 Google Closure Compiler 的高级模式,似乎只有在未包装在匿名包装器中时,才会内联小型简单函数。寻找我做错了什么的解释/解决方案或提示。
这是我的测试:
function Test() {
}
Test.prototype.div = function (index) {
return Math.floor(index / 32);
};
Test.prototype['test'] = function (index) {
return this.div(index);
};
window['Test'] = Test;
结果是这个小脚本,其中 div 函数被内联:
function a() { }
a.prototype.test = function(b) { return Math.floor(b / 32) };
window.Test = a;
接下来,测试的包装如下:
(function () { // <-- added
function Test() {
}
Test.prototype.div = function (index) {
return Math.floor(index / 32);
};
Test.prototype['test'] = function (index) {
return this.div(index);
};
window['Test'] = Test;
}()); // <-- added
div
函数是未内联:
(function() {
function a() { }
a.prototype.a = function(a) { return Math.floor(a / 32) };
a.prototype.test = function(a) { return this.a(a) };
window.Test = a
})();
是否有一些我不知道的副作用阻止了内联?
更新 1:我正在使用带有高级模式的在线编译器 +漂亮的印刷打勾。
更新2:发现命令行参数--output_wrapper
可以用作解决方法,--output_wrapper "(function() {%output%} )();"
。
I'm experimenting with advanced mode of Google Closure Compiler and it seems like small simple functions are inlined only when not wrapped in an anonymous wrapper. Looking for an explanation/solution or hint of what I'm doing wrong.
This is my test:
function Test() {
}
Test.prototype.div = function (index) {
return Math.floor(index / 32);
};
Test.prototype['test'] = function (index) {
return this.div(index);
};
window['Test'] = Test;
which results in this small script where the div
function is inlined:
function a() { }
a.prototype.test = function(b) { return Math.floor(b / 32) };
window.Test = a;
Next, the test is wrapped like:
(function () { // <-- added
function Test() {
}
Test.prototype.div = function (index) {
return Math.floor(index / 32);
};
Test.prototype['test'] = function (index) {
return this.div(index);
};
window['Test'] = Test;
}()); // <-- added
The div
function is not inlined:
(function() {
function a() { }
a.prototype.a = function(a) { return Math.floor(a / 32) };
a.prototype.test = function(a) { return this.a(a) };
window.Test = a
})();
Is there some side-effect that I'm not aware of that is preventing inlining here?
Update 1: I'm using online compiler with advanced-mode + pretty-print ticked.
Update 2: Found out that the command line parameter --output_wrapper
can be used as a workaround, --output_wrapper "(function() {%output%})();"
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是内联,而是原型虚拟化
这是一个已知问题:关闭编译器问题
This is not inlining, but prototype virtualization
It is a known issue: Closure compiler issue
不,除了在编译器中执行内联的逻辑之外,没有什么可以阻止内联。
Nope, nothing preventing inlining except logic to do it in the compiler.