6 Functions
This chapter covers
- Examining functions as objects with internal slots that determine their capabilities
- Tracing the four-step mechanism behind the new keyword
- Distinguishing this binding in regular functions versus arrow functions
- Revealing closures as heap-allocated Context objects with real memory costs
- Following V8's optimization pipeline and the cost of type inconsistency
The first five chapters built your mental model of JavaScript from the ground up. You've seen how numbers hide IEEE 754 complexity, how strings balance immutability with efficiency, and how objects rely on hidden classes to make property access fast. At each step, we looked past syntax to understand what the engine actually does.
This chapter marks a shift. From here forward, we're done asking "what is X?" You know what functions are. You've written thousands of them.
But knowing the syntax doesn't explain how things work internally and does not prevent bugs. You've stared at code where this was undefined for no apparent reason. You've watched a Node.js app slowly consume memory until it crashed. You've written functions that ran fine in development but crawled in production. These problems share a common root: the gap between how functions appear to work and how they actually work.