什么是Dart“Expando”功能,它做什么? [英] What is the Dart "Expando" feature about, what does it do?
问题描述
最近在Dart中使用了术语Expando。听起来不错。 API不能为我提供很多线索。
Have been seeing the term "Expando" used recently with Dart. Sounds interesting. The API did not provide much of a clue to me.
一两个例子可能是最有帮助的!
An example or two could be most helpful!
(不确定这是否相关,但我最担心的方法是将方法(getter)和/或变量添加到类中。解决这个问题的关键(提示:我现在使用Nosuchmethod方法,并希望能够返回未定义方法的值。))
(Not sure if this is related, but I am most anxious for a way to add methods (getters) and/or variables to a class. Hoping this might be a key to solving this problem. (hint: I am using the Nosuchmethod method now and want to be able to return the value of the unfound method.))
提前感谢,
_swarmii
推荐答案
Expandos允许将对象对象。一个非常有用的例子是HTML DOM元素,它本身不能被子类化。让我们做一个顶层expando为一个元素添加一些功能 - 在这种情况下是typedef语句中给出的函数签名:
Expandos allow you to associate objects to other objects. One very useful example of this is an HTML DOM element, which cannot itself be sub-classed. Let's make a top-level expando to add some functionality to an element - in this case a Function signature given in the typedef statement:
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');
}
这篇关于什么是Dart“Expando”功能,它做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!