達特不允許添加或者在運行時從類的實例中移除成員變量。在重寫你的飛鏢例如它可能是這個樣子:
class doStuff {
bool isDefined;
doStuff() {
isDefined = true;
}
void stuff() {
print('The function stuff was called!');
}
}
main() {
new doStuff().stuff();
}
如果你想一個屬性包添加到達特一個類你可以這樣寫:
class PropertyObject {
Map<String, Dynamic> properties;
PropertyObject() {
properties = new Map<String, Dynamic>();
}
Dynamic operator[](String K) => properties[K];
void operator[]=(String K, Dynamic V) => properties[K] = V;
}
main() {
PropertyObject bag = new PropertyObject();
bag['foo'] = 'world';
print('Hello ${bag['foo']}');
}
請注意,你不能訪問使用'。'映射屬性運營商。
我很驚訝地發現從Javascript到Dart的轉換比原始的Javascript版本更加冗長。 –
另一種方法是實現Function接口: – Cutch