Enumeration
JSValue
@dynamicMemberLookup public enum JSValue
JSValue represents a value in JavaScript.
Relationships
Conforms To
CustomStringConvertibleEquatableExpressibleByFloatLiteralExpressibleByIntegerLiteralExpressibleByNilLiteralExpressibleByStringLiteralJSValueCompatible
Initializers
init(stringLiteral:)
public init(stringLiteral value: String)
init(integerLiteral:)
public init(integerLiteral value: Int32)
init(floatLiteral:)
public init(floatLiteral value: Double)
init(nilLiteral:)
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.
jsString
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.
isNull
var isNull: Bool
Returns the true if this JS value is null.
If not, returns false.
isUndefined
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?
jsValue()
public func jsValue() -> JSValue
fromJSValue()
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()
isInstanceOf(_:)
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
| Name | Type | Description |
|---|---|---|
| constructor | JSFunction |
The constructor function to check. |
Returns
The result of instanceof in the JavaScript environment.