Skip to content

JSExportedDartFunction

extension type JSExportedDartFunction(JSExportedDartFunctionRepType) implements JSFunction

Annotations: @JS('Function')

A JavaScript callable function created from a Dart function.

See FunctionToJSExportedDartFunction.toJS or FunctionToJSExportedDartFunction.toJSCaptureThis for more details on how to convert a Dart function.

Properties

hashCode no setter inherited

int get hashCode

The hash code for this object.

A hash code is a single integer which represents the state of the object that affects operator == comparisons.

All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).

If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.

Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.

Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.

If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.

Inherited from Object.

Implementation
dart
external int get hashCode;

isDefinedAndNotNull extension no setter

bool get isDefinedAndNotNull

Available on JSAny, provided by the NullableUndefineableJSAnyExtension extension

Implementation
dart
bool get isDefinedAndNotNull => !isUndefinedOrNull;

isNull extension no setter

bool get isNull

Whether this value corresponds to JavaScript null.

INFO

Currently, there is no way to distinguish between JavaScript undefined and JavaScript null when compiling to Wasm. Therefore, this getter should only be used for code that compiles to JavaScript and will throw when compiling to Wasm.

Available on JSAny, provided by the NullableUndefineableJSAnyExtension extension

Implementation
dart
external bool get isNull;

isTruthy extension no setter

JSBoolean get isTruthy

The result of <code>!!this</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSBoolean get isTruthy;

isUndefined extension no setter

bool get isUndefined

Whether this value corresponds to JavaScript undefined.

INFO

Currently, there is no way to distinguish between JavaScript undefined and JavaScript null when compiling to Wasm. Therefore, this getter should only be used for code that compiles to JavaScript and will throw when compiling to Wasm.

Available on JSAny, provided by the NullableUndefineableJSAnyExtension extension

Implementation
dart
external bool get isUndefined;

isUndefinedOrNull extension no setter

bool get isUndefinedOrNull

Available on JSAny, provided by the NullableUndefineableJSAnyExtension extension

Implementation
dart
bool get isUndefinedOrNull => this == null;

not extension no setter

JSBoolean get not

The result of <code>!this</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSBoolean get not;

runtimeType no setter inherited

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

toDart extension no setter

Function get toDart

The Dart Function that this JSExportedDartFunction wrapped.

Must be a function that was wrapped with FunctionToJSExportedDartFunction.toJS or FunctionToJSExportedDartFunction.toJSCaptureThis.

Available on JSExportedDartFunction, provided by the JSExportedDartFunctionToFunction extension

Implementation
dart
external Function get toDart;

Methods

add() extension

JSAny add(JSAny? any)

The result of <code>this + any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSAny add(JSAny? any);

and() extension

JSAny? and(JSAny? any)

The result of <code>this && any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSAny? and(JSAny? any);

callAsConstructor() extension

R callAsConstructor<R>([JSAny? arg1, JSAny? arg2, JSAny? arg3, JSAny? arg4])

Calls this JSFunction as a constructor with up to four arguments.

Returns the constructed object, which must be an R.

This helper doesn't allow passing nulls, as it determines whether an argument is passed based on whether it was null or not. Prefer callAsConstructorVarArgs if you need to pass nulls.

Available on JSFunction, provided by the JSFunctionUnsafeUtilExtension extension

Implementation
dart
&#47;&#47; TODO(srujzs): The type bound should extend `JSObject`.
R callAsConstructor<R>([
  JSAny? arg1,
  JSAny? arg2,
  JSAny? arg3,
  JSAny? arg4,
]) => _callAsConstructor(arg1, arg2, arg3, arg4) as R;

callAsConstructorVarArgs() extension

R callAsConstructorVarArgs<R extends JSObject>([List<JSAny?>? arguments])

Calls this JSFunction as a constructor with a variable number of arguments.

Returns the constructed JSObject, which must be an R.

Available on JSFunction, provided by the JSFunctionUnsafeUtilExtension extension

Implementation
dart
R callAsConstructorVarArgs<R extends JSObject>([List<JSAny?>? arguments]) =>
    _callAsConstructorVarArgs(arguments) as R;

callAsFunction() extension

JSAny? callAsFunction([
  JSAny? thisArg,
  JSAny? arg1,
  JSAny? arg2,
  JSAny? arg3,
  JSAny? arg4,
])

Call this JSFunction using the JavaScript .call syntax and returns the result.

Takes at most 4 args for consistency with other APIs and relative brevity. If more are needed, you can declare your own external member with the same syntax.

Available on JSFunction, provided by the JSFunctionUtilExtension extension

Implementation
dart
&#47;&#47; We rename this function since declaring a `call` member makes a class
&#47;&#47; callable in Dart. This is convenient, but unlike Dart functions, JavaScript
&#47;&#47; functions explicitly take a `this` argument (which users can provide `null`
&#47;&#47; for in the case where the function doesn't need it), which may lead to
&#47;&#47; confusion.
&#47;&#47; https:&#47;&#47;developer.mozilla.org&#47;en-US&#47;docs&#47;Web&#47;JavaScript&#47;Reference&#47;Global_Objects&#47;Function&#47;call
@JS('call')
external JSAny? callAsFunction([
  JSAny? thisArg,
  JSAny? arg1,
  JSAny? arg2,
  JSAny? arg3,
  JSAny? arg4,
]);

callMethod() extension

R callMethod<R extends JSAny?>(
  JSAny method, [
  JSAny? arg1,
  JSAny? arg2,
  JSAny? arg3,
  JSAny? arg4,
])

Calls method on this JSObject with up to four arguments.

Returns the result of calling method, which must be an R.

This helper doesn't allow passing nulls, as it determines whether an argument is passed based on whether it was null or not. Prefer callMethodVarArgs if you need to pass nulls.

Available on JSObject, provided by the JSObjectUnsafeUtilExtension extension

Implementation
dart
R callMethod<R extends JSAny?>(
  JSAny method, [
  JSAny? arg1,
  JSAny? arg2,
  JSAny? arg3,
  JSAny? arg4,
]) => _callMethod(method, arg1, arg2, arg3, arg4) as R;

callMethodVarArgs() extension

R callMethodVarArgs<R extends JSAny?>(JSAny method, [List<JSAny?>? arguments])

Calls method on this JSObject with a variable number of arguments.

Returns the result of calling method, which must be an R.

Available on JSObject, provided by the JSObjectUnsafeUtilExtension extension

Implementation
dart
R callMethodVarArgs<R extends JSAny?>(
  JSAny method, [
  List<JSAny?>? arguments,
]) => _callMethodVarArgs(method, arguments) as R;

dartify() extension

Object? dartify()

Converts a JavaScript JSON-like value to the Dart equivalent if possible.

Effectively the inverse of NullableObjectUtilExtension.jsify, dartify takes a JavaScript JSON-like value and recursively converts it to a Dart object, doing the following:

  • If the value is a string, number, boolean, null, undefined, DataView or a typed array, does the equivalent toDart operation if it exists and returns the result.
  • If the value is a simple JS object (the protoype is either null or JS Object), creates and returns a [Map]<Object?, Object?> whose keys are the recursively converted keys obtained from Object.keys and its values are the associated values of the keys in the JS object.
  • If the value is a JS Array, each item in it is recursively converted and added to a new [List]<Object?>, which is then returned.
  • Otherwise, the conversion is undefined.

If the value contains a cycle, the behavior is undefined.

INFO

Prefer using the specific conversion member like toDart if you know the JavaScript type as this method may perform many type-checks. You should generally call this method with values that only contain JSON-like values as the conversion may be platform- and compiler-specific otherwise.

Available on JSAny, provided by the JSAnyUtilityExtension extension

Implementation
dart
&#47;&#47; TODO(srujzs): We likely need stronger tests for this method to ensure
&#47;&#47; consistency. We should also limit the accepted types in this API to avoid
&#47;&#47; confusion. Once the conversion for unrelated types is consistent across all
&#47;&#47; backends, we can update the documentation to say that the value is
&#47;&#47; internalized instead of the conversion being undefined.
external Object? dartify();

delete() extension

JSBoolean delete(JSAny property)

Deletes the property with key property from this JSObject.

Uses JavaScript's delete to delete the property.

Available on JSObject, provided by the JSObjectUnsafeUtilExtension extension

Implementation
dart
external JSBoolean delete(JSAny property);

divide() extension

JSAny divide(JSAny? any)

The result of <code>this / any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSAny divide(JSAny? any);

equals() extension

JSBoolean equals(JSAny? any)

The result of <code>this == any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSBoolean equals(JSAny? any);

exponentiate() extension

JSAny exponentiate(JSAny? any)

The result of <code>this ** any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSAny exponentiate(JSAny? any);

getProperty() extension

R getProperty<R extends JSAny?>(JSAny property)

The value of the property key property of this JSObject.

Available on JSObject, provided by the JSObjectUnsafeUtilExtension extension

Implementation
dart
external R getProperty<R extends JSAny?>(JSAny property);

greaterThan() extension

JSBoolean greaterThan(JSAny? any)

The result of <code>this > any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSBoolean greaterThan(JSAny? any);

greaterThanOrEqualTo() extension

JSBoolean greaterThanOrEqualTo(JSAny? any)

The result of <code>this >= any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSBoolean greaterThanOrEqualTo(JSAny? any);

has() extension

bool has(String property)

Shorthand helper for hasProperty to check whether this JSObject contains the property key property, but takes and returns a Dart value.

Available on JSObject, provided by the JSObjectUnsafeUtilExtension extension

Implementation
dart
bool has(String property) => hasProperty(property.toJS).toDart;

hasProperty() extension

JSBoolean hasProperty(JSAny property)

Whether or not this JSObject contains the property key property.

Uses JavaScript's in to check.

Available on JSObject, provided by the JSObjectUnsafeUtilExtension extension

Implementation
dart
external JSBoolean hasProperty(JSAny property);

instanceof() extension

bool instanceof(JSFunction constructor)

Whether this <code>JSAny?</code> is an instanceof constructor.

Available on JSAny, provided by the JSAnyUtilityExtension extension

Implementation
dart
external bool instanceof(JSFunction constructor);

instanceOfString() extension

bool instanceOfString(String constructorName)

Whether this <code>JSAny?</code> is an instanceof the constructor that is defined by constructorName, which is looked up in the globalContext.

If constructorName contains '.'s, the name is split into several parts in order to get the constructor. For example, library1.JSClass would involve fetching library1 off of the globalContext, and then fetching JSClass off of library1 to get the constructor.

If constructorName is empty or any of the parts or the constructor don't exist, returns false.

Available on JSAny, provided by the JSAnyUtilityExtension extension

Implementation
dart
bool instanceOfString(String constructorName) {
  if (constructorName.isEmpty) return false;
  final parts = constructorName.split('.');
  JSObject? constructor = globalContext;
  for (final part in parts) {
    constructor = constructor?[part] as JSObject?;
    if (constructor == null) return false;
  }
  return instanceof(constructor as JSFunction);
}

isA() extension

bool isA<T extends JSAny?>()

Whether this <code>JSAny?</code> is an instance of the JavaScript type that is declared by T.

Since the type-check this function emits is determined at compile-time, T needs to be an interop extension type that can also be determined at compile-time. In particular, isA can't be provided a generic type variable as a type argument.

This method uses a combination of null, typeof, and instanceof checks in order to do this check. Use this instead of is checks.

If T is a primitive JS type like JSString, this uses a typeof check that corresponds to that primitive type like typeofEquals('string').

If T is a non-primitive JS type like JSArray or an interop extension type on one, this uses an instanceof check using the name or the <code>@JS</code> rename of the given type like instanceOfString('Array'). Note that if you rename the library using the <code>@JS</code> annotation, this uses the rename in the instanceof check like instanceOfString('library1.JSClass').

To determine the JavaScript constructor to use as the second operand in the instanceof check, this function uses the JavaScript name associated with the extension type, which is either the argument given to the <code>@JS</code> annotation or the Dart declaration name. So, if you had an interop extension type JSClass that wraps JSArray without a rename, this does an instanceOfString('JSClass') check and not an instanceOfString('Array') check.

There are a few values for T that are exceptions to this rule:

  • JSTypedArray: As TypedArray does not exist as a class in JavaScript, this does some prototype checking to make isA<JSTypedArray> do the right thing.
  • JSBoxedDartObject: isA<JSBoxedDartObject> will check if the value is a result of a previous ObjectToJSBoxedDartObject.toJSBox call.
  • JSAny: If you do an isA<JSAny> check, it will only check for null.
  • User interop types whose representation types are JS primitive types: This will result in an error to avoid confusion on whether the user interop type is used in the type-check. Use the primitive JS type as the value for T instead.
  • User interop types that have an object literal constructor: This will result in an error as you likely want to use JSObject instead.

Available on JSAny, provided by the JSAnyUtilityExtension extension

Implementation
dart
@Since('3.4')
external bool isA<T extends JSAny?>();

lessThan() extension

JSBoolean lessThan(JSAny? any)

The result of <code>this < any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSBoolean lessThan(JSAny? any);

lessThanOrEqualTo() extension

JSBoolean lessThanOrEqualTo(JSAny? any)

The result of <code>this <= any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSBoolean lessThanOrEqualTo(JSAny? any);

modulo() extension

JSAny modulo(JSAny? any)

The result of <code>this % any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSAny modulo(JSAny? any);

multiply() extension

JSAny multiply(JSAny? any)

The result of <code>this * any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSAny multiply(JSAny? any);

noSuchMethod() inherited

dynamic noSuchMethod(Invocation invocation)

Invoked when a nonexistent method or property is accessed.

A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).

Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.

A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:

dart
class MockList<T> implements List<T> {
  noSuchMethod(Invocation invocation) {
    log(invocation);
    super.noSuchMethod(invocation); // Will throw.
  }
}
void main() {
  MockList().add(42);
}

This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.

If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.

The default behavior is to throw a NoSuchMethodError.

Inherited from Object.

Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

notEquals() extension

JSBoolean notEquals(JSAny? any)

The result of <code>this != any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSBoolean notEquals(JSAny? any);

or() extension

JSAny? or(JSAny? any)

The result of <code>this || any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSAny? or(JSAny? any);

setProperty() extension

void setProperty(JSAny property, JSAny? value)

Write the value of property key property of this JSObject.

Available on JSObject, provided by the JSObjectUnsafeUtilExtension extension

Implementation
dart
external void setProperty(JSAny property, JSAny? value);

strictEquals() extension

JSBoolean strictEquals(JSAny? any)

The result of <code>this === any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSBoolean strictEquals(JSAny? any);

strictNotEquals() extension

JSBoolean strictNotEquals(JSAny? any)

The result of <code>this !== any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSBoolean strictNotEquals(JSAny? any);

subtract() extension

JSAny subtract(JSAny? any)

The result of <code>this - any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
external JSAny subtract(JSAny? any);

toString() inherited

String toString()

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Inherited from Object.

Implementation
dart
external String toString();

typeofEquals() extension

bool typeofEquals(String typeString)

Whether the result of typeof on this <code>JSAny?</code> is typeString.

Available on JSAny, provided by the JSAnyUtilityExtension extension

Implementation
dart
external bool typeofEquals(String typeString);

unsignedRightShift() extension

JSNumber unsignedRightShift(JSAny? any)

The result of <code>this >>> any</code> in JavaScript.

Available on JSAny, provided by the JSAnyOperatorExtension extension

Implementation
dart
&#47;&#47; TODO(srujzs): This should return `num` or `double` instead.
external JSNumber unsignedRightShift(JSAny? any);

Operators

operator ==() inherited

bool operator ==(Object other)

The equality operator.

The default behavior for all Objects is to return true if and only if this object 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.

  • 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 whether two objects are equal should only change if at least one of the objects was modified.

If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.

Inherited from Object.

Implementation
dart
external bool operator ==(Object other);

operator extension

JSAny? operator [](String property)

Shorthand helper for getProperty to get the value of the property key property of this JSObject, but takes a Dart value.

Available on JSObject, provided by the JSObjectUnsafeUtilExtension extension

Implementation
dart
JSAny? operator [](String property) => getProperty(property.toJS);

operator []=() extension

void operator []=(String property, JSAny? value)

Shorthand helper for setProperty to write the value of the property key property of this JSObject, but takes a Dart value.

Available on JSObject, provided by the JSObjectUnsafeUtilExtension extension

Implementation
dart
void operator []=(String property, JSAny? value) =>
    setProperty(property.toJS, value);