liveBook · Manning

Chapter 6: Functions for the journeyman: understanding function invocation

What you will learn in chapter 6 of Secrets of the JavaScript Ninja, Third Edition

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

What it builds on

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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

Concepts explained in this chapter

← Chapter 5: First-class functions for the novice: definitions and arguments Chapter 7: Functions for the master: closures and scopes →

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.