JSFunction

public final class JSFunction<T> : JSExpression where T : Decodable

A JavaScript expression that executes a function in the current JavaScript this. The function variable is referenced by a key path relative to the current this.

For instance, to present an alert:

 let alert = JSFunction<JSVoid>("window.alert", arguments: "Hello from Swift!")
 // equivalent to the JS script: `this.window.alert("Hello from Swift!");`

Instances of this class are specialized with the T generic parameter. It must be set to the return type of the JavaScript function to execute. Check the documentation of the JavaScript function to know what to set the parameter to.

T must be a Decodable type. This includes:

  • JSVoid for functions that do not return a value
  • Primitive values (Strings, Numbers, Booleans, …)
  • Decodable enumerations
  • Objects decodable from JSON
  • Arrays of primitive values
  • Arrays of enumeration cases
  • Arrays of objects
  • Native dictionaries
  • Declaration

    Swift

    public typealias ReturnType = T
  • The key path to the function to execute, relative the current this object tree.

    Declaration

    Swift

    public let keyPath: String
  • The arguments to pass to the function.

    Declaration

    Swift

    public let arguments: [Encodable]
  • Creates a new method description.

    For instance, to present an alert:

     let alert = JSFunction<JSVoid>("window.alert", arguments: "Hello from Swift!")
     // equivalent to the JS script: `this.window.alert("Hello from Swift!");`
    

    Declaration

    Swift

    public init(_ keyPath: String, arguments: Encodable...)

    Parameters

    keyPath

    A dot-separated key path to the function to execute, relative the current this object tree.

    arguments

    The arguments to pass to the function. You can omit this paramter if the JavaScript function you are calling takes no arguments.

  • Creates the JavaScript text of the expression.

    Declaration

    Swift

    public func makeExpressionString() throws -> String