This MWE显示了Google封闭编译器如何与较长的obj[keyA]
交换短obj["some-very-long-key"]
:
输入javascript:
var foo = new function() {
var keyA = 'some-very-long-key';
var keyB = 'another-key';
this.bar = function() {
obj[keyA] = {};
}
this.baz = function(data) {
obj[keyA][keyB] = data;
}
}();
Google闭包编译器输出:
var foo = new function() {
this.bar = function() {
obj["some-very-long-key"] = {};
};
this.baz = function(a) {
obj["some-very-long-key"]["another-key"] = a;
};
};
如果我删除了包装功能,it works as I expected it to:
输入javascript:
var keyA = 'some-very-long-key';
var keyB = 'another-key';
function bar() {
obj[keyA] = {};
}
function baz(data) {
obj[keyA][keyB] = data;
}
Google闭包编译器输出:
var keyA = "some-very-long-key", keyB = "another-key";
function bar() {
obj[keyA] = {};
}
function baz(a) {
obj[keyA][keyB] = a;
}
;
因为我在项目中经常使用长键,所以代码变得比实际大,如果谷歌闭包编译器将字符串文字保留在变量中。
更新1:我知道我想要的结果可能表现得更差,但我宁愿选择更短的代码。