Skip to main content
100%

Gist 97d75332d2b298eab79a8849008bce9c

✓ Published0🌍 Public
RRestuta
Last edited Oct 24, 2017
Created on Oct 24, 2017

This example demonstrates how a closure in JavaScript can capture a mutable variable, causing a seemingly pure function to change behavior over time. The code defines `fn1`, which returns an object with `get` and `set` methods that share access to the parameter `a`. Initially, `get()` returns the string `"hello"`, but after calling `set()`, the same `get()` returns `"Good bye!"`, and the outer variable `a` is also updated. The visualization shows this state change through console output, using plain JavaScript without external libraries.

AI-generated description

Similar vizzes