Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have a Dart class (foo.dart):

class Foo {
  void talk() {
    print('Hello');
  }
}

After compiling foo.dart to JavaScript, I'd like to be able to use Foo like this:

var foo = new Foo(); // from foo.dart.js
foo.talk() // prints "Hello"

My questions:

  • Is this currently possible?
  • If so, how?
  • If not, what plans, if any, are in place to make it possible?

The dart:js library documentation states:

This library does not yet make Dart objects usable from JavaScript, their methods and proeprties [sic] are not accessible, though it does allow Dart functions to be passed into and called from JavaScript.

That word "yet" offers some hope, but I've found very little on this topic anywhere else.

Edit:

I do realize it's possible to call Dart functions from JavaScript using dart2js. However, what I'm trying to do is somewhat different. I'd like to be able to access all of the functionality of a Dart class from JavaScript.

share|improve this question
    
1  

1 Answer 1

Due to tree-shaking and minification this is normally not possible. If you have a Dart application (with a main() then you can make a Dart function available to be called from JavaScript (see How to call a Dart function from Javascript? for an example).

As far as I know there are plans to support your requirement but I have no idea about progress or when such a feature might be available.

This is the related project https://github.com/dart-lang/js-interop

share|improve this answer
    
The js-interop package looks like it requires a Dart vm. Is this true? I ask because the documentation test .html file uses Dart script github.com/dart-lang/… – Rich Apodaca Dec 5 '14 at 18:31
    
You have to run pub build in the project directory to build it to JavaScript. – Günter Zöchbauer Dec 5 '14 at 18:32

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.