Library Modulejavascriptkit 0.24.0JavaScriptKit

JavaScriptKit

Swift framework to interact with JavaScript through WebAssembly.

Documentation.md
import JavaScriptKit

Module information

Declarations
297
Symbols
426

Coverage

51.2 percent of the declarations in JavaScriptKit are fully documented41.8 percent of the declarations in JavaScriptKit are indirectly documented7.1 percent of the declarations in JavaScriptKit are completely undocumented

Declarations

2.0 percent of the declarations in JavaScriptKit are global functions or variables1.3 percent of the declarations in JavaScriptKit are operators34.0 percent of the declarations in JavaScriptKit are initializers, type members, or enum cases35.7 percent of the declarations in JavaScriptKit are instance members5.1 percent of the declarations in JavaScriptKit are instance subscripts2.4 percent of the declarations in JavaScriptKit are functors2.4 percent of the declarations in JavaScriptKit are protocols4.0 percent of the declarations in JavaScriptKit are protocol requirements3.4 percent of the declarations in JavaScriptKit are default implementations1.3 percent of the declarations in JavaScriptKit are structures5.7 percent of the declarations in JavaScriptKit are classes2.7 percent of the declarations in JavaScriptKit are typealiases

Interfaces

99.0 percent of the declarations in JavaScriptKit are unrestricted1.0 percent of the declarations in JavaScriptKit are SPI (unknown)
Module stats and coverage details

Overview

JavaScriptKit provides a seamless way to interact with JavaScript from Swift code when compiled to WebAssembly.

Quick Start

Check out the Quick Start: Hello World tutorial for a step-by-step guide to getting started.

Key Features

  • Access JavaScript objects and functions

  • Create closures that can be called from JavaScript

  • Convert between Swift and JavaScript data types

  • Use JavaScript promises with Swift’s async/await

  • Work with multi-threading

Example

import JavaScriptKit

// Access global JavaScript objects
let document = JSObject.global.document

// Create and manipulate DOM elements
var div = document.createElement("div")
div.innerText = "Hello from Swift!"
_ = document.body.appendChild(div)

// Handle events with Swift closures
var button = document.createElement("button")
button.innerText = "Click me"
button.onclick = .object(JSClosure { _ in
    JSObject.global.alert!("Button clicked!")
    return .undefined
})
_ = document.body.appendChild(button)

Check out the examples for more detailed usage.

Tutorials

  • Quick Start: Hello World

    This tutorial walks you through creating a simple web application using JavaScriptKit. You’ll learn how to set up a Swift package, add JavaScriptKit as a dependency, write code to manipulate the DOM, and build and run your web application.

    Read More

Core Types

  • enum JSValue

    JSValue represents a value in JavaScript.

  • class JSObject

    JSObject represents an object in JavaScript and supports dynamic member lookup. Any member access like object.foo will dynamically request the JavaScript and Swift runtime bridge library for a member with the specified name in this object.

  • class JSClosure

    JSClosure represents a JavaScript function the body of which is written in Swift. This type can be passed as a callback handler to JavaScript functions.