we (web engine): Experimental web browser project to understand the limits of Claude

JS Functions, closures, and var/let/const scoping #96

open opened by pierrelf.com

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)
  • arguments object (non-strict mode)
  • Rest parameters (...args)
  • Default parameter values
  • Arrow functions (lexical this, no arguments, no new)

this Binding#

  • Default binding (global or undefined in strict mode)
  • Implicit binding (method call: obj.method())
  • Explicit binding (call, apply, bind)
  • new binding (newly created object)
  • Arrow function: inherits this from 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 function
  • let/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
  • this is 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
  • var is function-scoped with hoisting
  • let/const are block-scoped with TDZ enforcement
  • const assignment throws TypeError
  • this binding follows all four rules
  • call/apply/bind work correctly
  • Rest parameters and default values work
  • arguments object 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.

sign up or login to add to the discussion
Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:meotu43t6usg4qdwzenk4s2t/sh.tangled.repo.issue/3mhn3mfmffa2k