我试图让这片代码工作的:关闭编译不适@const这个
/** @constructor */
function Foo()
{
/** @const */
this.bar = 5;
// edit: does now work
// this.bar = 3;
}
var f = new Foo();
// should be inlined (like other constants)
alert(f.bar);
我已经尝试加入更多的注释(类型,构造函数),中@enum
代替@const
(用于this.bar
),me = this
所有这些都没有任何影响。
help page对此没有什么帮助。
有没有办法让这个工作? 如果不是,为什么?
当前缀'@ const'注释时,变量将被内联(称为[constant propagation] //en.wikipedia.org/wiki/Constant_propagation))通过Closure编译器,这对于正常的'var'语句非常有效,这就是我想用属性重现的行为,它与你描述的过程有任何关系 – copy 2012-04-02 18:55:32
正如我所说的,编译器有不支持这个。它会在类属性时检查@const注释,但在执行优化时实际使用该信息会更保守。 – John 2012-04-02 23:20:24
我多花了一些时间与编译器一起玩,但认为你是对的;似乎没有办法做到这一点。无论如何,我现在正在使用cpp。 – copy 2012-04-06 17:22:26