Skip to content

playground

Any program in the subset

Nothing here is graded or recorded. Write something, step it, and watch what the machine does with it.

← → to step, Home and End for the ends

Program

1const base = { tags: [] };
2
3function withTag(options, tag) {
4 const merged = { ...base, ...options };
5 merged.tags.push(tag);
6 return merged;
7}
8
9const a = withTag({ name: "a" }, "first");
10const b = withTag({ name: "b" }, "second");
11console.log(a.tags.length, b.tags.length, a.tags === b.tags);

Scope chain

no scope entered yet

Call stack

  1. (top level)

Heap

no objects yet

Trace

  1. 1enter scope, creating base, a, b, withTag
  2. 2allocate array(0)
  3. 3allocate object
  4. 4base = object
  5. 5read withTag
  6. 6allocate object
  7. 7call withTag
  8. 8enter scope, creating merged
  9. 9read base
  10. 10read options
  11. 11allocate object
  12. 12merged = object
  13. 13read merged
  14. 14read tags
  15. 15read push, found on the prototypethe own object does not have it; the lookup walked the prototype chain
  16. 16read tag
  17. 17read merged
  18. 18return object
  19. 19a = object
  20. 20read withTag
  21. 21allocate object
  22. 22call withTag
  23. 23enter scope, creating merged
  24. 24read base
  25. 25read options
  26. 26allocate object
  27. 27merged = object
  28. 28read merged
  29. 29read tags
  30. 30read push, found on the prototypethe own object does not have it; the lookup walked the prototype chain
  31. 31read tag
  32. 32read merged
  33. 33return object
  34. 34b = object
  35. 35read console
  36. 36read log
  37. 37read a
  38. 38read tags
  39. 39read length
  40. 40read b
  41. 41read tags
  42. 42read length
  43. 43read a
  44. 44read tags
  45. 45read b
  46. 46read tags
  47. 47console.log: 2 2 true

Output

nothing logged yet