Something went wrong. Try again.
javascript for 🐜's, a tiny runtime with big ambitions antjs.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960// Test 1: Basic shadowing in nested scopeslet x = "outer";{ let x = "inner"; console.log(x); // Should print "inner"}console.log(x); // Should print "outer"
// Test 2: Closure capturing outer scopelet y = 10;function makeCounter() { let count = 0; return function() { count = count + 1; return count + y; // Access both local and outer scope };}let counter = makeCounter();console.log(counter()); // Should print 11console.log(counter()); // Should print 12
// Test 3: Variable shadowing in functionlet z = "global";function testShadow() { let z = "local"; return z;}console.log(testShadow()); // Should print "local"console.log(z); // Should print "global"
// Test 4: Nested function closuresfunction outer() { let a = 1; function middle() { let b = 2; function inner() { let c = 3; return a + b + c; // Access all three scopes } return inner(); } return middle();}console.log(outer()); // Should print 6
// Test 5: Multiple closures sharing same outer scopefunction makeCounters() { let shared = 0; return { inc: function() { shared = shared + 1; return shared; }, dec: function() { shared = shared - 1; return shared; }, get: function() { return shared; } };}let counters = makeCounters();console.log(counters.inc()); // Should print 1console.log(counters.inc()); // Should print 2console.log(counters.dec()); // Should print 1console.log(counters.get()); // Should print 1