Expando を使用すると、オブジェクトを他のオブジェクトに関連付けることができます。これの非常に便利な例の 1 つは、それ自体をサブクラス化できない HTML DOM 要素です。トップレベルの expando を作成して、要素にいくつかの機能を追加しましょう。この場合、typedef ステートメントで指定された Function シグネチャです。
typedef CustomFunction(int foo, String bar);
Expando<CustomFunction> domFunctionExpando = new Expando<CustomFunction>();
今それを使用するには:
main(){
// Assumes dart:html is imported
final myElement = new DivElement();
// Use the expando on our DOM element.
domFunctionExpando[myElement] = someFunc;
// Now that we've "attached" the function to our object,
// we can call it like so:
domFunctionExpando[myElement](42, 'expandos are cool');
}
void someFunc(int foo, String bar){
print('Hello. $foo $bar');
}