JSVariable

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

A JavaScript expression that returns the value of a variable in the current JavaScript this. The variable is referenced by a key path relative to the current this.

For instance, to get the title of the current document:

 let title = JSVariable<String>("document.title")
 // equivalent to the JS script: `this.document.title;`

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

T must be a Decodable type. This includes:

  • 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 path to the variable, relative to the current this object tree.

    Declaration

    Swift

    public let keyPath: String
  • Creates a new JavaScript variable description.

    For instance, to get the title of the current document:

     let title = JSVariable<String>("document.title")
     // equivalent to the JS script: `this.document.title;`
    

    Declaration

    Swift

    public init(_ keyPath: String)

    Parameters

    keyPath

    The dot-separated path to the variable, relative to the current this object tree.

  • Creates the JavaScript text of the expression.

    Declaration

    Swift

    public func makeExpressionString() -> String