JavaScriptKit Documentation

Enumeration JSValue

@dynamicMemberLookup
public enum JSValue: Equatable  

JSValue represents a value in JavaScript.

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

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) 

symbol

case symbol(JSSymbol) 

big​Int

case bigInt(JSBigInt) 

Properties

array

var array: JSArray?  

js​Value

public var jsValue: JSValue  

boolean

public var boolean: Bool?  

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

string

public 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

public var jsString: JSString?  

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

number

public var number: Double?  

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

object

public var object: JSObject?  

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

function

public var function: JSFunction?  

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

symbol

public var symbol: JSSymbol?  

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

big​Int

public var bigInt: JSBigInt?  

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

is​Null

public var isNull: Bool  

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

is​Undefined

public var isUndefined: Bool  

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

description

public var description: String  

Methods

construct(from:​)

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

from​JSValue()

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

string(_:​)

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

function(_:​)

@available(*, deprecated, message: "Please create JSClosure directly and manage its lifetime manually.")
    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()

function(_:​)

@available(*, deprecated, renamed: "object", message: "JSClosure is no longer a subclass of JSFunction. Use .object(closure) instead.")
    static func function(_ closure: JSClosure) -> JSValue  

is​Instance​Of(_:​)

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.