liveBook · Manning

Function

As explained in JavaScript in Depth

A JavaScript object representing callable function behavior and associated function metadata.

A function can have properties, yet `example()` works and `({})()` throws. Both values are objects; the difference is an internal capability. A function inherits from `Function.prototype` and has a `[[Call]]` slot that makes it callable. `typeof` reports `"function"`, but that label does not make functions a separate kind of property container. section 6.1

An object you can call

Define `function example() {}` and set `example.customProperty = 'hello'`. `example instanceof Object` is true, and `Object.keys(example)` can list the assigned property. Calling `example()` also works; calling an ordinary object does not. section 6.1

Other inaccessible slots account for other behavior. `[[Construct]]` permits construction with `new`; arrow functions lack it, so `new` on an arrow throws. `[[Environment]]` links a function to the scope where it was created, enabling closures. You cannot read or assign these slots in JavaScript. section 6.1

Table 6.1 The four forms of functions create very real differences in behavior
Capability
Declaration
Expression
Arrow
Class
Has [[Construct]]
✓
✓
✗
✓
Has own this binding
✓
✓
✗
✓
Has arguments object
✓
✓
✗
✓
Has prototype property
✓
✓
✗
✓
Hoisted
✓
 
✗
✗
Requires new
✗
✗
N/A
✓

Compare declarations, expressions, arrows, and classes by capability, not just syntax. In particular, an arrow has no own `this` binding or `arguments` object, while a class requires `new`. section 6.2

Call site matters for `this`. A regular function receives its `this` value according to how it is called, rather than where it was written; an arrow captures `this` from its surrounding scope when created. With `new`, construction creates an object, links its prototype, calls the constructor with that object as `this`, then returns it unless the constructor returns a different object. section 6.3 section 6.7

Function behavior is specified by JavaScript; execution speed depends on the engine. V8 can pre-parse a definition and defer full parsing and bytecode generation until its first call. Repeated calls may move through Ignition, Sparkplug or Maglev, and TurboFan. Type feedback guides optimization; inconsistent inputs can invalidate assumptions and trigger deoptimization. These are V8 implementation details, not a promise that every runtime uses the same pipeline. section 6.6.1 section 6.6.2

When a function is slow, first establish that it is the source of the cost. Under load, inspect a CPU profile's bottom-up view by self-time, then check `--trace-deopt` for repeated optimization bailouts. A hot function without deoptimization or inflated garbage-collection cost may simply do expensive work; inspect the algorithm before changing its call patterns. section 11.3.2

How JavaScript in Depth covers it

How deep it goes
Explains inaccessible internal slots and function-form differences, then traces V8 parsing and compilation without treating its pipeline as universal.
Examples
Uses an assigned function property, a function-form capability table, and CPU profiles with deoptimization traces.
What it assumes
Assumes familiarity with writing functions and objects, while explaining the slots behind behavior that syntax alone does not reveal.
What you can do after
Diagnose/debug with function capabilities, call-site binding rules, CPU profiles, and V8 deoptimization traces.
How it connects
Connects functions to prototypes, constructors, `this` binding, closures, type feedback, and deoptimization.

Also explained in

Compare how 26 books explain function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.