JavaScriptKit Documentation Beta

Enumeration JSValue

@dynamicMemberLookup public enum JSValue

JSValue represents a value in JavaScript.

%3 JSValue JSValue CustomStringConvertible CustomStringConvertible JSValue->CustomStringConvertible ExpressibleByStringLiteral ExpressibleByStringLiteral JSValue->ExpressibleByStringLiteral ExpressibleByIntegerLiteral ExpressibleByIntegerLiteral JSValue->ExpressibleByIntegerLiteral Equatable Equatable JSValue->Equatable ExpressibleByNilLiteral ExpressibleByNilLiteral JSValue->ExpressibleByNilLiteral JSValueCompatible JSValueCompatible JSValue->JSValueCompatible ExpressibleByFloatLiteral ExpressibleByFloatLiteral JSValue->ExpressibleByFloatLiteral

Conforms To

CustomStringConvertible
Equatable
ExpressibleByFloatLiteral
ExpressibleByIntegerLiteral
ExpressibleByNilLiteral
ExpressibleByStringLiteral
JSValueCompatible

Initializers

init(string​Literal:​)

public init(stringLiteral value: String)

init(integer​Literal:​)

public init(integerLiteral value: Int32)

init(float​Literal:​)

public init(floatLiteral value: Double)

init(nil​Literal:​)

public init(nilLiteral: ())

Enumeration Cases

boolean

case boolean(: Bool)

string

case string(: JSString)

number

case number(: Double)

object

case object(: JSObject)

null

case null

undefined

case undefined

function

case function(: JSFunction)

Properties

array

var array: JSArray?

boolean

var boolean: Bool?

Returns the Bool value of this JS value if its type is boolean. If not, returns nil.

string

var string: String?

Returns the String value of this JS value if the type is string. If not, returns nil.

Note that this accessor may copy the JS string value into Swift side memory.

To avoid the copying, please consider the jsString instead.

js​String

var jsString: JSString?

Returns the JSString value of this JS value if the type is string. If not, returns nil.

number

var number: Double?

Returns the Double value of this JS value if the type is number. If not, returns nil.

object

var object: JSObject?

Returns the JSObject of this JS value if its type is object. If not, returns nil.

function

var function: JSFunction?

Returns the JSFunction of this JS value if its type is function. If not, returns nil.

is​Null

var isNull: Bool

Returns the true if this JS value is null. If not, returns false.

is​Undefined

var isUndefined: Bool

Returns the true if this JS value is undefined. If not, returns false.

description

var description: String

Methods

construct(from:​)

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

js​Value()

public func jsValue() -> JSValue

from​JSValue()

public func fromJSValue<Type>() -> Type? where Type: ConstructibleFromJSValue

string(_:​)

public static func string(_ value: String) -> JSValue

function(_:​)

@available(*, deprecated, message: "Please create JSClosure directly and manage its lifetime manually.") public static func function(_ body: @escaping ([JSValue]) -> JSValue) -> JSValue

Deprecated: Please create JSClosure directly and manage its lifetime manually.

Migrate this usage

button.addEventListener!("click", JSValue.function { _ in
    ...
    return JSValue.undefined
})

into below code.

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

button.addEventListener!("click", JSValue.function(eventListenter))
...
button.removeEventListener!("click", JSValue.function(eventListenter))
eventListenter.release()

is​Instance​Of(_:​)

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

Return true if this value is an instance of the passed constructor function. Returns false for everything except objects and functions.

Parameters

constructor JSFunction

The constructor function to check.

Returns

The result of instanceof in the JavaScript environment.