liveBook · Manning

Chapter 4: Chapter 4. Functions for the journeyman: understanding function invocation

What you will learn in chapter 4 of Secrets of the JavaScript Ninja, Second Edition

Introduces the implicit function parameters `this` and `arguments` and explains that how a function is invoked influences their values.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the implicit function parameters `this` and `arguments` and explains that how a function is invoked influences their values.

Key terms: implicit function parameters, this, arguments, function context

4.1 Using implicit function parameters

This section introduces `arguments` and `this` as parameters usually available to a function without being named in its definition.

Key terms: implicit parameters, arguments, this, function signature

4.1 Do you know?

Poses three diagnostic questions about function context, methods, and constructor return values.

4.1.1 The arguments parameter

This section explains how the arguments object exposes every argument passed to a function, why it is not an array, and how strict mode changes its relationship with named parameters.

Key terms: arguments object, arguments.length, function parameters, variadic functions, array-like, rest parameter, array methods, alias, nonstrict mode, strict mode

4.1.2 The this parameter: introducing the function context

Introduces `this` as an implicit function parameter and explains why its value cannot be inferred solely from where a function is defined.

Key terms: this parameter, implicit parameter, function context, invoked as a method

4.2 Invoking functions

This section introduces four ways to invoke a JavaScript function and explains that the invocation form affects its function context.

Key terms: this parameter, function context, invoked, function, method, constructor, apply, call, function invocation operator, function reference

4.2.1 Invocation as a function

This section defines invocation as a function and explains how strict mode changes the value of this for that invocation.

Key terms: invocation as a function, () operator, method invocation, function context, this, nonstrict mode, strict mode, global context, window object

4.2.2 Invocation as a method

This section explains how calling a function through an object property makes the object its function context, and how one function can serve as a method on multiple objects.

Key terms: method, function context, this parameter, global context, nonstrict mode, function reference, this

4.2.3 Invocation as a constructor

This section explains how calling a function with `new` creates and initializes an object, how explicit return values affect the result, and why constructor functions are written and used differently from ordinary functions.

Key terms: constructor function, new, this parameter, function context, constructor return values, new expression, nonobject, normal functions, nonstrict mode, strict mode

4.2.4 Invocation with the apply and call methods

This section explains why a function can receive an unexpected `this` context and how `call` and `apply` let code choose that context when invoking a function.

Key terms: function context, this, event handler, target element, apply, call, invocation arguments, callback function, iteration entry, loop index

4.3 Fixing the problem of function contexts

This introduction identifies unexpected function context in callbacks as a problem and previews two further ways to address it.

Key terms: function context, callback functions, event handlers, call, apply

4.3.1 Using arrow functions to get around function contexts

This section shows how arrow functions retain the `this` value from where they are created, fixing one button-click callback but causing a subtle bug in an object-literal version.

Key terms: arrow function, this parameter, Button constructor, callback function, object literal, global code, window object, clicked property

4.3.2 Using the bind method

This section explains how bind creates a new function whose this value stays tied to a chosen object, illustrated with a button-click handler.

Key terms: bind method, bound function, this parameter, function context

4.4 Summary

4.5 Exercises

Where this is used later

Concepts explained in this chapter

← Chapter 3: Chapter 3. First-class functions for the novice: definitions and arguments Chapter 5: Chapter 5. 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.