The js.dart library provides simple JavaScript invocation from Dart that works on both Dartium and on other modern browsers via Dart2JS.
It provides a model based on scoped JsObject
objects. Proxies give Dart code access to JavaScript objects, fields, and functions as well as the ability to pass Dart objects and functions to JavaScript functions. Scopes enable developers to use proxies without memory leaks - a common challenge with cross-runtime interoperation.
The top-level context
getter provides a JsObject
to the global JavaScript context for the page your Dart code is running on. In the following example:
import 'dart:js'; void main() { context.callMethod('alert', ['Hello from Dart via JavaScript']); }context
'alert'
creates a proxy to the top-level alert function in JavaScript. It is invoked from Dart as a regular function that forwards to the underlying JavaScript one. By default, proxies are released when the currently executing event completes, e.g., when main is completes in this example.
The library also enables JavaScript proxies to Dart objects and functions. For example, the following Dart code:
context['dartCallback'] = new Callback.once((x) => print(x*2));defines a top-level JavaScript function 'dartCallback' that is a proxy to the corresponding Dart function. The
Callback.once
constructor allows the proxy to the Dart function to be retained across multiple events; instead it is released after the first invocation. (This is a common pattern for asychronous callbacks.)
Note, parameters and return values are intuitively passed by value for primitives and by reference for non-primitives. In the latter case, the references are automatically wrapped and unwrapped as proxies by the library.
This library also allows construction of JavaScripts objects given a JsObject
to a corresponding JavaScript constructor. For example, if the following JavaScript is loaded on the page:
function Foo(x) { this.x = x; } Foo.prototype.add = function(other) { return new Foo(this.x + other.x); }then, the following Dart:
var foo = new JsObject(context['Foo'], [42]); var foo2 = foo.callMethod('add', [foo]); print(foo2['x']);will construct a JavaScript Foo object with the parameter 42, invoke its add method, and return a
JsObject
to a new Foo object whose x field is 84.
Converts a json-like data
to a JavaScript map or array and return a JsObject
to it.
Converts a local Dart function to a callback that can be passed to JavaScript.
A JsObject
subtype to JavaScript functions.
Proxies to JavaScript objects.
Constructs a JsObject
to a new JavaScript object by invoking a (proxy to a) JavaScript constructor
. The arguments
list should contain either primitive values, DOM elements, or Proxies.
The equality operator.
The default behavior for all Object
s is to return true if and only if this
and other
are the same object.
Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:
null
.
o
, o == o
must be true.o1
and o2
, o1 == o2
and o2 == o1
musto1
, o2
, and o3
, if o1 == o2
ando2 == o3
are true, then o1 == o3
must be true.
The method should also be consistent over time, so equality of two objects should not change over time, or at least only change if one of the objects was modified.
If a subclass overrides the equality operator it should override the hashCode
method as well to maintain consistency.
Delete the name
property.
Check if this JsObject
has a name
property.
Check if this JsObject
is instance of type
.
Returns a string representation of this object.
Marker class used to indicate it is serializable to js. If a class is a Serializable
the "toJs" method will be called and the result will be used as value.