Station 08 · State, events & handlers
Until now you have built bones, mixed paint, and choreographed motion. None of those things knew when you clicked. JavaScript is what makes the boat listen. Think of it as the wiring under the decks: a button is hooked to a small recipe, the recipe edits a tiny notebook the boat keeps about itself, and the display reads that notebook to decide what to show.
Below is a working miniature theater, complete with its own script. Push the controls. Watch the values inside the state object update under your hands, and watch the boat answer. The recipes never move — only the values do, and the display follows.
1// the boat's whole memory of right now2let stage = {3curtain: "closed",4reel: "Captain's welcome",5lights: "up",6};78// recipes the buttons are wired to9function raise() {10stage.curtain = "open";11stage.lights = "down";12render();13}1415function lower() {16stage.curtain = "closed";17stage.lights = "up";18render();19}2021function pick(reel) {22stage.reel = reel;23render();24}
Hover any line of code, or any part of the stage — both light up together.
Wired to raise()
Three controls, one tiny script. Every click runs a recipe; every recipe edits the same little box of state; the boat just shows whatever the box says. That loop runs every interactive piece of this site — only larger.