JavaScriptKit Documentation Beta

Class JSClosure

public class JSClosure: JSFunction

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. Note that the lifetime of JSClosure should be managed by users manually due to GC boundary between Swift and JavaScript. For further discussion, see also swiftwasm/JavaScriptKit #33

e.g.

let eventListenter = JSClosure { _ in
    ...
    return JSValue.undefined
}

button.addEventListener!("click", JSValue.function(eventListenter))
...
button.removeEventListener!("click", JSValue.function(eventListenter))
eventListenter.release()
%3 JSClosure JSClosure JSFunction JSFunction JSClosure->JSFunction

Superclass

JSFunction

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

Initializers

init(_:​)

public init(_ body: @escaping ([JSValue]) -> JSValue)

Instantiate a new JSClosure with given function body.

Parameters

body @escaping ([JSValue]) -> JSValue

The body of this function.

init(_:​)

convenience public init(_ body: @escaping ([JSValue]) -> ())

A convenience initializer which assumes that the given body function returns JSValue.undefined

Methods

release()

public func release()

Release this function resource. After calling release, calling this function from JavaScript will fail.