简体   繁体   English

是否可以避免使用带有getter和setter的模式,而仍然使用Closure ADVANCED_OPTIMIZATIONS最小化JavaScript?

[英]Can a pattern with getters and setters be avoided and still minify JavaScript with Closure ADVANCED_OPTIMIZATIONS?

I would like to minify JavaScript using the Closure Compiler in ADVANCED_OPTIMIZATIONS mode. 我想在ADVANCED_OPTIMIZATIONS模式下使用Closure Compiler缩小JavaScript。 This is a pattern that I'm currently trying out: 这是我目前正在尝试的一种模式:

var myClosure = (function () {
    var NS = {};
    NS.foo = 100;
    var MyConstructor = function (aValue) {
        this.aValue = aValue;
    };
    NS["MyConstructor"] = MyConstructor;

    MyConstructor.prototype.setAValue = function (v) {
        this.aValue = v;
    };
    NS["MyConstructor"].prototype["setAValue"] = MyConstructor.prototype.setAValue;

    MyConstructor.prototype.readAValue = function () {
        return this.aValue;
    };
    NS["MyConstructor"].prototype["readAValue"] = MyConstructor.prototype.readAValue;

    return NS;
}());
window["myClosure"] = myClosure;

It works fine and I can call into the minified code with this: 它工作正常,我可以用以下代码调用缩小的代码:

var obj = new myClosure.MyConstructor(10);
alert(obj.readAValue());
obj.setAValue(100);
alert(obj.readAValue());

A JSFiddle to demonstrate. 一个JSFiddle进行演示。

While the above works, it will be very cumbersome to write all the getters and setters that this project will need in order to be able to call into the compiled code. 尽管上述工作有效,但是编写该项目将需要的所有getter和setter以便调用已编译的代码将非常麻烦。

Is there a pattern that I can use that will allow me to avoid using getter / setter methods? 我是否可以使用某种模式来避免使用getter / setter方法?

Or, if not, is there a way for me to judge if refactoring the code to get it to be callable with ADVANCED_OPTIMIZATIONS is going to be worth the effort. 或者,如果不是,是否有办法让我判断重构代码以使其可通过ADVANCED_OPTIMIZATIONS进行调用是否值得。 (I estimate that adding the methods will add over 5% in terms of number of lines to the uncompiled code) Also, I currently have about 1,300 QUnit assertion tests. (我估计添加方法将使未编译代码的行数增加5%以上)。此外,我目前有大约1,300个QUnit断言测试。 To run these tests, there are close to 10,000 assignments made that will have to be rewritten from something like: 要运行这些测试,需要进行将近10,000项作业,这些作业必须从以下内容中进行重写:

obj.ValueA = 100;
obj.ValueB = 200;

to

obj.SetValueA(100);
obj.SetValueB(200);

in order to test the minified code. 为了测试缩小的代码。

So, if possible, a different approach is definitely in order. 因此,如果可能的话,绝对有必要采用其他方法。

@expose was created for just this purpose. @expose创建了@expose

/** @expose */
MyConstructor.prototype.aValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有Closure Compiler和ADVANCED_OPTIMIZATIONS的模块模式 - Module pattern with Closure Compiler and ADVANCED_OPTIMIZATIONS 如何不编译goog.closure中的某些代码ADVANCED_OPTIMIZATIONS - How to not compile certain code in goog.closure ADVANCED_OPTIMIZATIONS 使用闭包编译器 ADVANCED_OPTIMIZATIONS 的奇怪对象属性行为 - strange object property behavior with closure compiler ADVANCED_OPTIMIZATIONS Google Closure Compiler ADVANCED_OPTIMIZATIONS - 排除所有函数名称 - Google Closure Compiler ADVANCED_OPTIMIZATIONS - Exclude All function names 是否可以使用闭包编译器ADVANCED_OPTIMIZATIONS与jQuery? - Is it possible to use Closure Compiler ADVANCED_OPTIMIZATIONS with jQuery? Google Closure编译器的ADVANCED_OPTIMIZATIONS选项 - Google Closure Compiler's ADVANCED_OPTIMIZATIONS option 如何在ADVANCED_OPTIMIZATIONS中的Closure Compiler中导出公共类方法? - How to export public class methods in Closure Compiler in ADVANCED_OPTIMIZATIONS? 关闭编译ADVANCED_OPTIMIZATIONS抱怨使用此方法 - closure compilation ADVANCED_OPTIMIZATIONS complaining about use of this 与Closure Compiler兼容的Java语言中的getter和setter的生成 - Generation of getters and setters in Javascript compatible with Closure Compiler 我可以使用显示模块模式在闭包中为变量使用getters / setter方法吗? - Can I use getters/setters for a variable in a closure using the revealing module pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM