Files
gemigreerd-js-maker/scrbl/usecases.scrbl
T
2026-06-08 12:55:08 +02:00

44 lines
1.1 KiB
Racket

#lang scribble/manual
@(require (for-label racket/base js-maker))
@title{js-maker use cases}
@section{Generating a small function}
@racketblock[
(js (define (square x)
(return (* x x))))
]
This produces a JavaScript function declaration. Racket identifiers are mapped
to JavaScript-friendly names by replacing unsupported characters with
underscores.
@section{Generating a loop}
@racketblock[
(js (define (sum-to n)
(let loop ([i 0] [acc 0])
(if (> i n)
(return acc)
(loop (+ i 1) (+ acc i))))))
]
The named @racket[let] form is useful for simple loops while keeping ordinary
Racket binding semantics for the initial values and loop updates.
@section{Generating DOM-style calls}
@racketblock[
(js
(define title (send document getElementById "title"))
(set! (js-dot title innerHTML) "Hello")
(send title addEventListener "click"
(lambda (evt) (return #t))))
]
This is string generation only. The generated JavaScript must still be run in a
JavaScript environment that provides the referenced objects, such as
@tt{document}.