Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:js
dart:js
description

dart:js library

Low-level support for interoperating with JavaScript.

Note

You should usually usedart:js_interop instead of this library.To learn more, check out theJS interop documentation.

This library provides access to JavaScript objects from Dart, allowingDart code to get and set properties, and call methods of JavaScript objectsand invoke JavaScript functions. The library takes care of convertingbetween Dart and JavaScript objects where possible, or providing proxies ifconversion isn't possible.

This library does not make Dart objects usable from JavaScript, theirmethods and properties are not accessible, though it does allow Dartfunctions to be passed into and called from JavaScript.

JsObject is the core type and represents a proxy of a JavaScript object.JsObject gives access to the underlying JavaScript objects properties andmethods.JsObjects can be acquired by calls to JavaScript, or they can becreated from proxies to JavaScript constructors.

The top-level gettercontext provides aJsObject that represents theglobal object in JavaScript, usuallywindow.

The following example shows an alert dialog via a JavaScript call to theglobal functionalert():

import 'dart:js';main() => context.callMethod('alert', ['Hello from Dart!']);

This example shows how to create aJsObject from a JavaScript constructorand access its properties:

import 'dart:js';main() {  var object = JsObject(context['Object']);  object['greeting'] = 'Hello';  object['greet'] = (name) => "${object['greeting']} $name";  var message = object.callMethod('greet', ['JavaScript']);  context['console'].callMethod('log', [message]);}

Proxying and automatic conversion

When setting properties on a JsObject or passing arguments to a JavaScriptmethod or function, Dart objects are automatically converted or proxied toJavaScript objects. When accessing JavaScript properties, or when a Dartclosure is invoked from JavaScript, the JavaScript objects are alsoconverted to Dart.

Functions and closures are proxied in such a way that they are callable. ADart closure assigned to a JavaScript property is proxied by a function inJavaScript. A JavaScript function accessed from Dart is proxied by aJsFunction, which has aJsFunction.apply method to invoke it.

The following types are transferred directly and not proxied:

  • Basic types:null,bool,num,String,DateTime
  • TypedData, including its subclasses likeInt32List, butnotByteBuffer
  • When compiling for the web, also:Blob,Event,ImageData,KeyRange,Node, andWindow.

Converting collections with JsObject.jsify()

To create a JavaScript collection from a Dart collection use theJsObject.jsify constructor, which converts DartMaps andIterablesinto JavaScript Objects and Arrays.

The following expression creates a new JavaScript object with the propertiesa andb defined:

var jsMap = JsObject.jsify({'a': 1, 'b': 2});

This expression creates a JavaScript array:

var jsArray = JsObject.jsify([1, 2, 3]);

Classes

JsArray<E>
AList that proxies a JavaScript array.
JsFunction
A proxy on a JavaScript Function object.
JsObject
A proxy on a JavaScript object.

Properties

contextJsObject
The JavaScript global object, usuallywindow.
no setter

Functions

allowInterop<F extendsFunction>(Ff)→ F
Returns a wrapper around functionf that can be called from JavaScriptusingpackage:js JavaScript interop.
allowInteropCaptureThis(Functionf)Function
Returns a wrapper around functionf that can be called from JavaScriptusingpackage:js JavaScript interop, passing JavaScriptthis as thefirst argument.
  1. Dart
  2. dart:js
DartSDK
  1. Libraries
  2. Core
  3. dart:async
  4. dart:collection
  5. dart:convert
  6. dart:core
  7. dart:developer
  8. dart:math
  9. dart:typed_data
  10. VM
  11. dart:ffi
  12. dart:io
  13. dart:isolate
  14. dart:mirrors
  15. Web
  16. package:webopen_in_new
  17. dart:js_interop
  18. dart:js_interop_unsafe
  19. Web (Legacy)
  20. dart:html
  21. dart:indexed_db
  22. dart:js
  23. dart:js_util
  24. dart:svg
  25. dart:web_audio
  26. dart:web_gl
dart:js library

[8]ページ先頭

©2009-2025 Movatter.jp