2

我在下面得到了这个 Dart 脚本,我想在使用 dart2js 编译 Dart 脚本后通过 JavaScript 访问类 hello_world 中的方法。有人知道这是如何工作的吗?!我已经知道如何访问诸如 foo(...) 之类的函数,这不是问题,但它与类和方法的工作方式不同。dartlang.org 上的教程只解释了如何访问函数,而不是方法和类。我不明白...

import 'dart:js' as js;

class hello_world {

  String hello = 'Hello World!';

  String getHello() {
    print("getHello!!!!!");
    return hello;
  }

  void ausgabe() {
    print("Hallo Welt");
    //return 0;
  }
}

String foo(int n) {
  print("hallo");

  void foo2() {
    print("hallo2");
  }

  //works
  js.context['foo2'] = foo2;
  return 'Hallo';
}


 void main() {

  int zahl1 = 3;
  int zahl2 = 1234;
  String w = 'test';

  hello_world test = new hello_world();

  //works
  js.context['foo'] = foo;   

}
4

1 回答 1

1

假设你想在 Dart 方法上创建一个 Js 函数绑定,你几乎可以做同样的事情:

void main() {
  hello_world test = new hello_world();

  // define a 'getHelloOnTest' Js function
  js.context['getHelloOnTest'] = test.getHello;
}

现在在 Js 方面你可以使用:

getHelloOnTest();
于 2014-03-12T13:53:56.693 回答