【发布时间】:2014-09-06 10:52:26
【问题描述】:
当使用 dart2js 编译为 JavaScript 时,使用 const 构造函数创建的类实例是否比普通实例(使用 new 构造函数创建)更优化?
【问题讨论】:
标签: javascript dart dart2js
当使用 dart2js 编译为 JavaScript 时,使用 const 构造函数创建的类实例是否比普通实例(使用 new 构造函数创建)更优化?
【问题讨论】:
标签: javascript dart dart2js
这里有 2 个元组实现:
使用常量构造函数:
class Tuple{
final _1, _2;
foo()=> _1 + _2;
const Tuple(this._1,this._2);
}
void main() {
var a = const Tuple(10,20);
var b = const Tuple(10,20);
print(a);
print(b);
print(a.foo());
}
使用新的构造函数:
class Tuple{
final _1, _2;
foo()=> _1 + _2;
Tuple(this._1,this._2);
}
void main() {
var a = new Tuple(10,20);
var b = new Tuple(10,20);
print(a);
print(b);
print(a.foo());
}
这就是new Tuple 在输出中的样子:
main: function() {
P.print(new S.Tuple(10, 20));
P.print(new S.Tuple(10, 20));
P.print(30);
}
const Tuple 只创建一次 C.Tuple_10_20 = new S.Tuple(10, 20); 并像这样使用:
main: function() {
P.print(C.Tuple_10_20);
P.print(C.Tuple_10_20);
P.print(C.Tuple_10_20._1 + C.Tuple_10_20._2);
}
请注意,在 new Tuple 的情况下,函数调用已被其返回值文字替换,但 const Tuple 输出并未发生这种情况。
【讨论】: