2

When passing Dart functions to Javascript code, the current convention is to use allowInterop() from package:js (presumably). This works pretty well but it creates a closure, which is not allowed by Javascript code that expects a constructor function.

I've done some digging, but arrive at a dead end here:

external DART_CLOSURE_TO_JS(Function function);

Where does this go? Is it open source?

Would it be possible for a Dart function to be converted to a non-closure JS function in either package:js or dart:js? If not, why?

2
  • Hi, do you have example of what you already tried to do ? Commented Oct 28, 2018 at 16:19
  • Defining custom elements requires passing a constructor function, and an error is thrown if that function is a closure. It is related to this issue. There are ways to make it work with either DDC or Dart2JS, but I'm not aware of any method that works for both. Commented Oct 28, 2018 at 17:55

1 Answer 1

4

A Dart function wouldn't work as a JS constructor, because it wouldn't have the proper prototype set up. Also with custom elements there's another difficulty: it has to delegate correctly to the DOM element constructor.

The normal way to do that is with an ES6 class. I made an example of using JS to define the custom element class. I think you may have seen, but linking it here just in case: Dart custom element interop demo. The file web/interop.js defines the custom element class.

I think an ES5 constructor would work too, if it used Reflect.construct(). I spent some time last week seeing if that would help retrofit dart:html to work with custom elements, but I don't think it's enough. Dart will need some better JS interop capabilities.

In the meantime, my suggestion is to define custom element classes in JS and use interop to talk to Dart (and vice versa).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.