Files
rust_browser/tests/external/js262/fixtures/arrow-function-outer-scope.js
Zachary D. Rowitsch 75bc30bb8e Add JavaScript arrow function (=>) support
Implement parsing, evaluation, lexical this capture, and new rejection
for arrow functions. Covers expression bodies, block bodies, zero/single/
multi-param forms, and backtracking disambiguation from grouping parens.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-21 22:12:47 -05:00

28 lines
785 B
JavaScript

// Arrow functions can read variables from their enclosing (outer) scope.
// Reading a global variable defined before the arrow.
var base = 10;
var addBase = x => x + base;
console.log(addBase(5));
// Multiple arrows can share the same outer variable.
var multiplier = 3;
var triple = x => x * multiplier;
var quadruple = x => x * 4;
console.log(triple(7));
console.log(quadruple(7));
// Arrow reads a variable that was updated after the arrow was defined.
var counter = 0;
var getCounter = () => counter;
counter = 99;
console.log(getCounter());
// Arrow passed to Array.forEach (using a hand-rolled loop) closes over outer variable.
var sum = 0;
var values = [1, 2, 3];
for (var i = 0; i < values.length; i++) {
(function(v) { sum = sum + v; })(values[i]);
}
console.log(sum);