Chapter 6: Functions for the journeyman: understanding function invocation
This chapter introduction establishes that `this` depends on how a function is called and explains why rest parameters have largely replaced the older `arguments` object.
After this chapter you will be able to
- Invocation-dependent this value. Explain that `this` is like an implicit function parameter whose value can vary with how the function is called, rather than being fixed for the function.
- The implicit this parameter. Explain that a function receives `this` in addition to its explicit arguments and that `this` refers to the object associated with the invocation. Describe how a class method can use it to refer to the instance it is called on.
- Invocation and method context. Explain why assigning a class method to a variable can lead to an error when it is called: the assignment alone is not the problem, because `this` can depend on how the function is invoked. Contrast the failing call, where `this` is undefined in strict mode, with the reassigned function that works when invoked differently.
- Invocation and function context. Explain that how a function is invoked determines how its this parameter, or function context, is established.
- Four function invocation forms. Describe the four invocation forms shown here: a standalone call, a method call tied to an object, a constructor call that brings a new object into being, and a call through apply or call.
- Standalone invocation form. Distinguish a standalone function call from a method call and other invocation forms described here.
- Standalone call this value. Describe the value of `this` in a non-arrow standalone function call under strict mode and the exceptions stated here.
- Method invocation and context. Explain how calling a non-arrow function through an object's property invokes it as a method and makes that object available as this. Describe how the same function can be attached to different objects and invoked as their method.
- New Object and Function Context. Trace what happens when an eligible function is called with new: an empty object is created, passed as this, and normally returned. Describe how the Ninja example adds a skulk method to that object.
- Explicit Constructor Return Values. Distinguish the results of explicitly returning an object versus a primitive from a constructor invoked with new, and state the section's advice about explicit returns.
- Explicit function context. Explain how call and apply let a function run with a chosen object as its function context, even when the function is not attached to that object.
- Apply and call argument forms. Distinguish the arguments supplied to apply from those supplied to call: both receive the context first, but apply receives an array of invocation arguments while call receives those arguments individually.
- Unbound method callbacks. Explain why passing buttonObject.clickHandler as a callback does not make the receiving function invoke it as a method of buttonObject, and relate that to the failed clickCount example.
- Bound function context. Explain that bind returns a new function with the same body whose this value stays set to the chosen object, regardless of how the new function is invoked. Describe why passing a bound click handler preserves its intended context.
- Arrow function lexical context. Explain that an arrow function takes its `this` from the surrounding context rather than from how it is invoked. In the Ninja constructor example, this lets `skulk` keep referring to the instance.
- Ordinary object context limitation. Distinguish an arrow function defined on an ordinary object from one defined in a context whose `this` is the intended instance. Explain why assigning an arrow as an object property does not directly make it a bound method.
What it builds on
- Arguments and function parameters (chapter 5) · Distinguishes function parameters from arguments supplied in a call.
- Rest parameters (chapter 5) · Introduces rest parameters.
- Functions as first-class objects (chapter 5) · Functions can be treated as values and assigned to variables.
- Defining functions (chapter 5) · Introduces functions before considering how they are invoked.
- Function declarations and function expressions (chapter 5) · Introduces function declarations and function expressions.
- Arrow functions (chapter 5) · Introduces arrow functions.
Section by section
Introduction
This chapter introduction establishes that `this` depends on how a function is called and explains why rest parameters have largely replaced the older `arguments` object.
- Invocation-dependent this value: Explain that `this` is like an implicit function parameter whose value can vary with how the function is called, rather than being fixed for the function.
Why it matters: This distinction is the starting point for understanding function invocation in this chapter. - Arguments object and rest parameters: Describe what the `arguments` object contains and why the book says its use fell out of fashion after rest parameters were introduced.
Why it matters: It explains why this book mentions `arguments` but uses rest parameters instead.
Key terms: this, implicit function parameter, arguments, array-like object, rest parameters
6.1 The this parameter: introducing the function context
This section introduces `this` as the function context and shows why a method’s behavior can change when it is invoked differently.
- The implicit this parameter: Explain that a function receives `this` in addition to its explicit arguments and that `this` refers to the object associated with the invocation. Describe how a class method can use it to refer to the instance it is called on.
Why it matters: It gives you a way to describe what a method is referring to when it uses `this`. - Invocation and method context: Explain why assigning a class method to a variable can lead to an error when it is called: the assignment alone is not the problem, because `this` can depend on how the function is invoked. Contrast the failing call, where `this` is undefined in strict mode, with the reassigned function that works when invoked differently.
Why it matters: It helps explain why moving a method into a variable can produce a surprising runtime error without making reassignment itself invalid.
Key terms: this, implicit parameter, function context, class methods, strict mode
6.2 Invoking functions
Introduces the four ways to invoke a function and explains that the invocation determines its function context.
- Invocation and function context: Explain that how a function is invoked determines how its this parameter, or function context, is established.
Why it matters: The way a function is called matters when reasoning about its context. - Four function invocation forms: Describe the four invocation forms shown here: a standalone call, a method call tied to an object, a constructor call that brings a new object into being, and a call through apply or call.
Why it matters: Recognizing the call form is the starting point for understanding function context. - Getter and setter invocation: State that getters and setters are called as methods, and that invoking one separately requires extracting it from its object first.
Why it matters: Getters and setters have a specific invocation constraint within the forms just introduced.
Key terms: this parameter, function context, invoked, standalone function, method, constructor, apply, call, getters, setters
6.2.1 Invocation as a standalone function
This section defines standalone function invocation and explains how strict mode affects `this` in a non-arrow standalone call.
- Standalone invocation form: Distinguish a standalone function call from a method call and other invocation forms described here.
Why it matters: Recognizing the call form helps you reason about the function’s context. - Standalone call this value: Describe the value of `this` in a non-arrow standalone function call under strict mode and the exceptions stated here.
Why it matters: The call form alone does not tell you `this` without considering strict mode and binding. - Strict mode context rationale: Explain why strict mode’s `this` behavior improves on the original standalone-call behavior and how code commonly gets strict mode.
Why it matters: It explains why the section treats strict-mode behavior as the practical default.
Key terms: standalone function, method invocation, this, strict mode, bind, global object, non-arrow function, use strict, ECMAScript modules (ESM)
6.2.2 Invocation as a method
Explains when an object-property call is treated as a method invocation, how it sets this, and the stated limits on that behavior.
- Method invocation and context: Explain how calling a non-arrow function through an object's property invokes it as a method and makes that object available as this. Describe how the same function can be attached to different objects and invoked as their method.
Why it matters: It explains why this can refer to the object through which a function is called. - Method context limitations: Distinguish the function forms and call notation that the section says receive the attached object as this from those that do not.
Why it matters: These stated exceptions keep the method-context rule from being applied to every property call.
Key terms: method, function context, this parameter, function declaration, function expression, arrow functions, bound functions, dot notation, bracket notation
6.2.3 Invocation as a constructor
Explains how invoking a function with new creates an object, sets its function context, and determines the returned value.
- Constructor Invocation and Eligibility: Distinguish constructor invocation from ordinary invocation: new makes an eligible function act as a constructor. State that function declarations and function expressions can be used this way, but arrow and bound functions cannot.
Why it matters: It clarifies why the same eligible function can be called in different ways. - New Object and Function Context: Trace what happens when an eligible function is called with new: an empty object is created, passed as this, and normally returned. Describe how the Ninja example adds a skulk method to that object.
Why it matters: This explains how a constructor initializes the object that a caller receives. - Explicit Constructor Return Values: Distinguish the results of explicitly returning an object versus a primitive from a constructor invoked with new, and state the section's advice about explicit returns.
Why it matters: Explicit returns are an exception to the usual rule that new gives back the object supplied as this. - Detecting Constructor Calls: Explain why checking this instanceof func does not reliably establish that func was called with new, and identify new.target as the reliable check described here.
Why it matters: It separates evidence about the value of this from evidence about how the function was invoked.
Key terms: constructor, function declaration, function expression, arrow functions, bound functions, new, this parameter, function context, skulk, return
6.2.4 Invocation with the apply and call methods
This section explains how call and apply invoke a function with an explicitly chosen this value, how their argument syntax differs, and why they remain useful.
- Explicit function context: Explain how call and apply let a function run with a chosen object as its function context, even when the function is not attached to that object.
Why it matters: You can choose the object a function acts on without making the function that object's method. - Apply and call argument forms: Distinguish the arguments supplied to apply from those supplied to call: both receive the context first, but apply receives an array of invocation arguments while call receives those arguments individually.
Why it matters: This distinction tells you which method fits the arguments you have. - Modern uses of call and apply: Explain why apply is less necessary for passing an array as an argument list today, while call and apply remain useful for borrowing a method for another object.
Why it matters: It helps you recognize when call or apply solves a context problem rather than merely passing an array of arguments.
Key terms: function context, this, apply, call, first-class objects, invocation arguments, spread operator, borrow a method
6.3 Fixing the problem of function contexts
This section explains why passing an object’s method as a callback can give it an unexpected function context.
- Unbound method callbacks: Explain why passing buttonObject.clickHandler as a callback does not make the receiving function invoke it as a method of buttonObject, and relate that to the failed clickCount example.
Why it matters: It explains why a callback can fail even though the method worked when called through its object.
Key terms: this, callback, unbound method, function context
6.3.1 Using the bind method
Explains how bind creates a new function with a fixed this context, then briefly introduces class fields as a way to define bound methods.
- Bound function context: Explain that bind returns a new function with the same body whose this value stays set to the chosen object, regardless of how the new function is invoked. Describe why passing a bound click handler preserves its intended context.
Why it matters: A bound handler can keep the intended this value even when another piece of code invokes it. - Class field bound methods: Describe the section’s brief introduction of class fields as a convenient syntax for defining bound methods on classes, noting the use of = instead of : after the function name.
Why it matters: It connects the usefulness of binding to the class syntax introduced here without requiring the later explanation of class fields.
Key terms: bind, bound function, this parameter, function context, bound methods, class fields, class constructors
6.3.2 Using arrow functions to get around function contexts
This section explains how arrow functions handle `this`, why that does not directly bind methods on ordinary objects, and how arrow wrappers can preserve a method call.
- Arrow function lexical context: Explain that an arrow function takes its `this` from the surrounding context rather than from how it is invoked. In the Ninja constructor example, this lets `skulk` keep referring to the instance.
Why it matters: It explains why an arrow callback can keep the intended `this` even when passed around or invoked differently. - Ordinary object context limitation: Distinguish an arrow function defined on an ordinary object from one defined in a context whose `this` is the intended instance. Explain why assigning an arrow as an object property does not directly make it a bound method.
Why it matters: It prevents the mistaken assumption that making an ordinary object's property an arrow function automatically binds it to that object. - Method invocation arrow wrappers: Describe the section's alternative of wrapping a complete method invocation in an arrow function, and explain why the wrapper must pass along any arguments the method needs.
Why it matters: Wrapping the invocation offers a concise alternative, but omitted arguments can change what the called method receives.
Key terms: arrow functions, lexical context, this, call, apply, bind, ordinary objects, clickHandler, bound method, class fields
6.4 Summary
Where this is used later
- Defining getters and setters (chapter 9) · Covers defining getters and setters in more detail.
- Building prototyped objects with constructors (chapter 9) · Constructor-based object creation relies on recognizing invocation with new.
- Classic OOP with class and extends (chapter 9) · Returns to classes, where the book says it will examine class fields more closely.
Concepts explained in this chapter
- thisJavaScript syntax that evaluates to a function's call-dependent or lexically captured this value.
- constructor methodA JavaScript function used to create and initialize an object, typically when invoked with the new operator.
- arrow functionThe => token used to define an arrow function in JavaScript.
- callA function invocation is an operation that calls a function and supplies zero or more arguments to it.
- applyA JavaScript method that invokes a function with an explicitly supplied this value and an array-like collection of arguments.
- callA JavaScript method that invokes a function with an explicitly supplied this value and individual arguments.
- new keywordThe new operator invokes a constructable function, creates an object for its this value, and returns the constructed object subject to explicit-return rules.
- methodA function associated with an object or class and typically invoked through that association.
- strict modeA JavaScript execution mode that applies stricter syntax and runtime semantics.
- first-class objectA value that can be assigned, stored, passed as an argument, and returned from a function.
- global objectThe top-level object that provides globally accessible bindings for a JavaScript execution environment.
- getterA getter is an object or class property accessor that computes and returns a value when the property is read.
- setterA setter is a property accessor that receives a value when the property is assigned.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
