什么是Dart“Expando”功能,它有什么作用?

时间:2012-11-13 09:11:03

标签: dart expando nosuchmethod

最近在Dart上看到过“Expando”一词。听起来不错。 API没有给我提供太多线索。

一两个例子可能是最有帮助的!

(不确定这是否相关,但我最担心的是将方法(getter)和/或变量添加到类中。希望这可能是解决这个问题的关键。(提示:我正在使用现在的Nosuchmethod方法,并希望能够返回未完成方法的值。))

提前致谢,

_swarmii

3 个答案:

答案 0 :(得分:13)

Expandos允许您将对象关联到其他对象。一个非常有用的例子是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');
}

答案 1 :(得分:7)

只是为了澄清expando和地图之间的区别:正如groups中所报告的那样,expando有弱引用
 这意味着密钥可以被垃圾收集,即使它仍然存在于expando中(只要没有其它引用)。

对于所有其他意图和目的,它是一张地图。

答案 2 :(得分:1)

我玩了一下。这就是我所拥有的。

import 'dart:html';

const String cHidden = 'hidden';

class ExpandoElement {
  static final Expando<ExpandoElement> expando =
      new Expando<ExpandoElement>("ExpandoElement.expando");

  final Element element;

  const ExpandoElement._expand(this.element);

  static Element expand(Element element) {
    if (expando[element] == null)
      expando[element] = new ExpandoElement._expand(element);
    return element;
  }

//  bool get hidden => element.hidden; // commented out to test noSuchMethod()
  void set hidden(bool hidden) {
    if (element.hidden = hidden)
      element.classes.add(cHidden);
    else
      element.classes.remove(cHidden);
  }

  noSuchMethod(InvocationMirror invocation) => invocation.invokeOn(element);
}
final Expando<ExpandoElement> x = ExpandoElement.expando;
Element xquery(String selector) => ExpandoElement.expand(query(selector));

final Element input = xquery('#input');

void main() {
  input.classes.remove(cHidden);
  assert(!input.classes.contains(cHidden));

  input.hidden = true;
  assert(x[input].hidden); // Dart Editor warning here, but it's still true
  assert(!input.classes.contains(cHidden)); // no effect

  input.hidden = false;
  assert(!x[input].hidden); // same warning, but we'll get input.hidden via noSuchMethod()
  assert(!input.classes.contains(cHidden));

  x[input].hidden = true;
  assert(input.hidden); // set by the setter of ExpandoElement.hidden
  assert(input.classes.contains(cHidden)); // added by the setter
  assert(x[input].hidden);
  assert(x[input].classes.contains(cHidden)); // this is input.classes

  x[input].hidden = false;
  assert(!input.hidden); // set by the setter
  assert(!input.classes.contains(cHidden)); // removed by the setter
  assert(!x[input].hidden);
  assert(!x[input].classes.contains(cHidden));

  // confused?
  assert(input is Element);
  assert(x[input] is! Element); // is not
  assert(x[input] is ExpandoElement);
  assert(x is Expando<ExpandoElement>);
}
相关问题