我一直在使用谷歌闭包,试图让大量的 JavaScript 干净地编译,以便使用谷歌编译器最小化。我遇到了一个问题:
goog.provide('test');
goog.provide('test2');
/**
* @constructor
*/
test = function () {
this.x = 10;
this.y = 13;
};
(function () {
/**
* @constructor
*/
test2 = function () {
this.x = 10;
this.y = 13;
};
})();
前者很好。后者产生一个常量重定义错误:
JSC_CONSTANT_REASSIGNED_VALUE_ERROR. constant test2 assigned a value more than once at /home/hbrown/tmp/closure-test/foo.js line 16 : 10
BUILD FAILED: 1 error(s), 0 warning(s)
有没有办法强制 plovr/closure 编译器允许这个构造?我环顾四周,一无所获。
后来:在更进一步的一点上,为什么闭包/plovr 认为 test2 是一个常数?我怀疑它与 plovr/closure 在调用 goog.provide 时为 test2 创建命名空间有关。很高兴看到它在生成错误时正在使用的中间形式。