Eloquent Javascript

Page 99

It is common for a program to do something a given number of times. You can write a for loop for that, like this: for (let i = 0; i < 10; i++) { console.log(i); }

Can we abstract “doing something N times” as a function? Well, it’s easy to write a function that calls console.log N times. function repeatLog(n) { for (let i = 0; i < n; i++) { console.log(i); } }

But what if we want to do something other than logging the numbers? Since “doing something” can be represented as a function and functions are just values, we can pass our action as a function value. function repeat(n, action) { for (let i = 0; i < n; i++) { action(i); } } repeat(3, console.log); // → 0 // → 1 // → 2

We don’t have to pass a predefined function to repeat. Often, it is easier to create a function value on the spot instead. let labels = []; repeat(5, i => { labels.push(`Unit ${i + 1}`); });

87


Turn static files into dynamic content formats.

Create a flipbook

Articles inside

Compatibility and the browser wars

0
page 231

In the sandbox

0
page 230

The standard

1min
page 234

HTML and JavaScript

1min
page 229

Exercises

4min
pages 221-223

Functions

3min
pages 218-219

The evaluator

1min
page 214

Asynchronous bugs

3min
pages 205-206

The environment

1min
page 217

Special forms

2min
pages 215-216

The event loop

1min
page 204

Generators

1min
page 203

Async functions

3min
pages 201-202

Message routing

4min
pages 198-200

Failure

3min
pages 192-193

Network flooding

1min
page 197

Networks are hard

4min
pages 194-195

Collections of promises

1min
page 196

Promises

1min
page 191

Callbacks

2min
pages 189-190

Crow tech

1min
page 188

Building and bundling

1min
page 181

ECMAScript modules

3min
pages 179-180

Improvised modules

1min
page 175

CommonJS

2min
pages 177-178

Evaluating data as code

1min
page 176

Packages

1min
page 174

Summary

3min
pages 169-170

International characters

1min
page 168

Parsing an INI file

2min
pages 166-167

Greed

1min
page 161

The lastIndex property

2min
pages 164-165

The replace method

1min
pages 159-160

Dynamically creating RegExp objects

1min
page 162

The search method

1min
page 163

Backtracking

2min
pages 157-158

Word and string boundaries

1min
page 155

The Date class

1min
page 154

Cleaning up after exceptions

3min
pages 141-142

Repeating parts of a pattern

1min
page 151

Exceptions

2min
pages 139-140

Assertions

1min
page 145

Selective catching

3min
pages 143-144

Error propagation

1min
page 138

Debugging

2min
pages 136-137

Testing

1min
page 135

Simulation

2min
pages 126-127

Types

0
page 134

Strict mode

0
page 133

Exercises

1min
page 131

Persistent data

1min
page 125

The task

2min
pages 123-124

Exercises

2min
pages 119-120

Summary

1min
page 118

Inheritance

1min
page 116

The iterator interface

2min
pages 112-113

Maps

2min
pages 108-109

Overriding derived properties

2min
pages 106-107

Class notation

1min
page 105

Classes

1min
page 104

Prototypes

2min
pages 102-103

Methods

1min
page 101

Exercises

0
page 99

Recognizing text

1min
page 97

Strings and character codes

2min
pages 95-96

Summary

1min
page 98

Composability

2min
pages 93-94

Summarizing with reduce

1min
page 92

Transforming with map

1min
page 91

Filtering arrays

1min
page 90

Exercises

3min
pages 82-84

Script data set

1min
page 89

5 Higher-Order Functions

1min
page 85

Higher-order functions

0
page 88

Summary

1min
page 81

JSON

1min
page 80

Destructuring

1min
page 79

The Math object

2min
pages 77-78

Rest parameters

1min
page 76

Strings and their properties

2min
pages 74-75

Further arrayology

1min
page 73

The final analysis

3min
pages 71-72

Array loops

0
page 70

Computing correlation

1min
page 69

The lycanthrope’s log

3min
pages 67-68

Mutability

2min
pages 65-66

Data sets

1min
page 60

Properties

1min
page 61

Objects

3min
pages 63-64

Methods

1min
page 62

Exercises

2min
pages 57-58

Summary

1min
page 56

Functions and side effects

1min
page 55

Growing functions

2min
pages 53-54

Recursion

3min
pages 50-52

Closure

1min
page 49

Optional Arguments

2min
pages 47-48

The call stack

1min
page 46

Arrow functions

1min
page 45

Declaration notation

1min
page 44

Functions as values

1min
page 43

Bindings and scopes

2min
pages 41-42

Exercises

2min
pages 38-39

Summary

1min
page 37

Dispatching on a value with switch

1min
page 35

Summary

0
page 21

Updating bindings succinctly

1min
page 34

Indenting Code

1min
page 32

while and do loops

2min
pages 30-31

Conditional execution

2min
pages 28-29

Strings

4min
pages 13-14

Bindings

3min
pages 23-24

Unary operators

1min
page 15
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.