Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Observers and geometry: the pattern versus host APIs

The observer pattern lets code notify registered callbacks about state changes. The example shows only how to explicitly log a rectangle’s current dimensions, without subscriptions or browser measurements.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const listeners = new Set();
listeners.add(callback);
for (const callback of listeners) callback(geometry);
listeners.delete(callback);

The label observers and geometry combines two concerns: change notifications and data describing shape or position. In the observer pattern, the code that manages state stores callbacks and invokes them according to agreed rules. The separate Set syntax sketch shows how to add, call, and remove those callbacks. The runnable example does not use that mechanism.

The example starts with width set to 10 and height set to 5. It then assigns 20 to width and calls notify() directly. The function reads the current values and appends the string 20x5 to the changes array. console.log(JSON.stringify(changes)) prints ["20x5"]. The assignment alone does not call notify(): the code does not compare dimensions, register callbacks, or support unsubscribing. The dimensions are values in variables, not measurements of a page element.

ECMAScript specifies the language and distinguishes it from its host environment. The cited sources are not sufficient to document the behavior of ResizeObserver, IntersectionObserver, MutationObserver, or getBoundingClientRect(). This entry neither assigns them an ECMAScript edition nor confirms browser support. The presence of Node 24 in the repository does not establish browser support either. Consult Web API documentation for the behavior and availability of these interfaces.

Examples

const changes = [];
let width = 10;
let height = 5;

function notify() {
  changes.push(`${width}x${height}`);
}

width = 20;
notify();
console.log(JSON.stringify(changes));

Result

["20x5"]

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources