JavaScriptKit Documentation

Class JSObject

@dynamicMemberLookup
public class JSObject: Equatable  

JSObject represents an object in JavaScript and supports dynamic member lookup. Any member access like object.foo will dynamically request the JavaScript and Swift runtime bridge library for a member with the specified name in this object.

And this object supports to call a member method of the object.

e.g.

let document = JSObject.global.document.object!
let divElement = document.createElement!("div")

The lifetime of this object is managed by the JavaScript and Swift runtime bridge library with reference counting system.

%101 JSObject JSObject Hashable Hashable JSObject->Hashable CustomStringConvertible CustomStringConvertible JSObject->CustomStringConvertible Equatable Equatable JSObject->Equatable JSFunction JSFunction JSFunction->JSObject JSOneshotClosure JSOneshotClosure JSOneshotClosure->JSObject JSSymbol JSSymbol JSSymbol->JSObject JSClosure JSClosure JSClosure->JSObject JSBigInt JSBigInt JSBigInt->JSObject

Subclasses

JSBigInt

A wrapper around the JavaScript BigInt class that exposes its properties in a type-safe and Swifty way.

JSOneshotClosure

JSOneshotClosure is a JavaScript function that can be called only once. This class can be used for optimized memory management when compared to the common JSClosure.

JSClosure

JSClosure represents a JavaScript function the body of which is written in Swift. This type can be passed as a callback handler to JavaScript functions.

JSFunction

JSFunction represents a function in JavaScript and supports new object instantiation. This type can be callable as a function using callAsFunction.

JSSymbol

A wrapper around the JavaScript Symbol class that exposes its properties in a type-safe and Swifty way.

Conforms To

CustomStringConvertible
Equatable
Hashable

Initializers

init(id:​)

@_spi(JSObject_id)
    public init(id: JavaScriptObjectRef)  

Properties

id

@_spi(JSObject_id)
    public var id: JavaScriptObjectRef

throwing

public var throwing: JSThrowingObject  

A modifier to call methods as throwing methods capturing this

const animal = {
  validateAge: function() {
    if (this.age < 0) {
      throw new Error("Invalid age");
    }
  }
}
let animal = JSObject.global.animal.object!
try animal.throwing.validateAge!()

global

public static let global  

A JSObject of the global scope object. This allows access to the global properties and global names by accessing the JSObject returned.

js​Value

public var jsValue: JSValue  

description

public var description: String  

Methods

is​Instance​Of(_:​)

public func isInstanceOf(_ constructor: JSFunction) -> Bool  

Return true if this value is an instance of the passed constructor function.

Parameters

constructor JSFunction

The constructor function to check.

Returns

The result of instanceof in the JavaScript environment.

construct(from:​)

public class func construct(from value: JSValue) -> Self?  

hash(into:​)

public func hash(into hasher: inout Hasher)  

Hashes the essential components of this value by feeding them into the given hasher.

Parameters

hasher inout Hasher

The hasher to use when combining the components of this instance.

Operators

==

public static func == (lhs: JSObject, rhs: JSObject) -> Bool  

Returns a Boolean value indicating whether two values point to same objects.

Parameters

lhs JSObject

A object to compare.

rhs JSObject

Another object to compare.