dart.js


Getters and Setters
context: JsObject
Functions
jsify(dynamic data): JsObject
Classes
Callback
JsFunction
JsObject
Serializable

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.

Getters and Setters

static JsObject get context

Returns a proxy to the global JavaScript context for this page.

Functions

static JsObject jsify(dynamic data)

Converts a json-like data to a JavaScript map or array and return a JsObject to it.


Class Callback implements Serializable<JsFunction>

Constructors
Callback(Function f)
Callback.withThis(Function f)
Methods
toJs(): JsFunction

Converts a local Dart function to a callback that can be passed to JavaScript.

Constructors

factory Callback(Function f)
factory Callback.withThis(Function f)

Methods

JsFunction toJs()

Class JsFunction extends JsObject implements Serializable<JsFunction>

Methods
apply(dynamic thisArg, List<dynamic> args): dynamic

A JsObject subtype to JavaScript functions.

Methods

dynamic apply(dynamic thisArg, List<dynamic> args)

Class JsObject implements Serializable<JsObject>

Fields
hashCode: int
Getters and Setters
hashCode: int
Constructors
JsObject(Serializable<JsFunction> constructor, List<dynamic> arguments)
Methods
==(dynamic other): dynamic
[](dynamic arg): dynamic
[]=(dynamic key, dynamic value): dynamic
callMethod(String name, List<dynamic> args): dynamic
deleteProperty(String name): void
hasProperty(String name): bool
instanceof(Serializable<JsFunction> type): bool
toJs(): JsObject
toString(): String

Proxies to JavaScript objects.

Fields

final int hashCode

Getters and Setters

int get hashCode

Constructors

factory JsObject(Serializable<JsFunction> constructor, List<dynamic> arguments)

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.

Methods

dynamic ==(dynamic other)

The equality operator.

The default behavior for all Objects 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:

  • Total: It must return a boolean for all arguments. It should never throw
or return null.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must
either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and
o2 == 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.

dynamic [](dynamic arg)
dynamic []=(dynamic key, dynamic value)
dynamic callMethod(String name, List<dynamic> args)
void deleteProperty(String name)

Delete the name property.

bool hasProperty(String name)

Check if this JsObject has a name property.

bool instanceof(Serializable<JsFunction> type)

Check if this JsObject is instance of type.

JsObject toJs()
String toString()

Returns a string representation of this object.


Abstract class Serializable

Constructors
Serializable()
Methods
toJs(): T

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.

Constructors

Serializable()

Methods

T toJs()