Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I got this Dart Script below and I want to access the methods from the class hello_world by JavaScript after I compiled the Dart Script with dart2js. Does anybody know how this works?! I already know how to access the functions like foo(...), thats not the problem, but it does not work the same way with classes and methods. And the tutorials on dartlang.org only explain how to access functions, not methods and classes. I dont get it...

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;   

}
share|improve this question
    
I think accessing the class is the same as regular function, isn't it? But to use any of its methods you have to instantiate an object first –  doodeec Mar 12 at 13:36
    
take a look at this question stackoverflow.com/questions/22099483 for more search for the tag dart-js-interop –  Günter Zöchbauer Mar 12 at 13:39

1 Answer 1

Assuming you want to create a Js function bind on a Dart method you can do almost the same thing :

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

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

Now on Js side you can use :

getHelloOnTest();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.