闭包编译器不会虚拟化匿名包装器中包含的原型函数

发布于 2024-12-20 12:00:05 字数 1408 浏览 3 评论 0原文

我正在尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

勿忘初心 2024-12-27 12:00:05

这不是内联,而是原型虚拟化

这是一个已知问题:关闭编译器问题

This is not inlining, but prototype virtualization

It is a known issue: Closure compiler issue

微暖i 2024-12-27 12:00:05

不,除了在编译器中执行内联的逻辑之外,没有什么可以阻止内联。

Nope, nothing preventing inlining except logic to do it in the compiler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文