[英]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.