Implement JavaScript function objects, lexical closures, and the full variable scoping model.
Scope#
Build function objects with closure support and implement var/let/const scoping rules per ECMAScript specification.
Function Objects#
- Function as a first-class value (callable object)
- Function.prototype with call, apply, bind
- Constructor calls (new operator)
argumentsobject (non-strict mode)- Rest parameters (...args)
- Default parameter values
- Arrow functions (lexical
this, noarguments, nonew)
this Binding#
- Default binding (global or undefined in strict mode)
- Implicit binding (method call: obj.method())
- Explicit binding (call, apply, bind)
newbinding (newly created object)- Arrow function: inherits
thisfrom enclosing scope
Closures#
- Capture variables from enclosing scope
- Environment chain: each function has reference to outer environment
- Captured variables are live references (not copies)
- Multiple closures can share the same environment
Scoping#
var: function-scoped, hoisted to top of functionlet/const: block-scoped, temporal dead zone (TDZ)const: immutable binding (assignment after init is TypeError)- Hoisting: function declarations hoisted entirely, var declarations hoisted (init to undefined)
- Scope chain: local → enclosing → ... → global
Strict Mode#
- "use strict" directive (function and script level)
- No implicit global variable creation
thisis undefined (not global) in default binding- No
arguments.callee
Acceptance Criteria#
- Function objects are callable and constructable
- Arrow functions with lexical
this - Closures correctly capture and mutate outer variables
-
varis function-scoped with hoisting -
let/constare block-scoped with TDZ enforcement -
constassignment throws TypeError -
thisbinding follows all four rules -
call/apply/bindwork correctly - Rest parameters and default values work
-
argumentsobject works in non-arrow functions - Unit tests for scoping, closures, this binding
Phase 10 — JavaScript Engine (issue 7 of 15). Depends on: JS Object model.