📅  最后修改于: 2023-12-03 15:00:35.442000             🧑  作者: Mango
Eloquent JavaScript is a comprehensive guide to the JavaScript programming language. It covers everything you need to know, from the basics to more advanced topics.
Some of the topics covered in the book include:
The book is written in a clear and engaging style, with plenty of examples and exercises to help you learn. It also includes online resources and a community forum where you can get help and advice from other readers.
Here's an example of a function that uses a closure to calculate a running total:
function makeCounter() {
let count = 0;
return function() {
count++;
return count;
};
}
let counter1 = makeCounter();
console.log(counter1()); // 1
console.log(counter1()); // 2
console.log(counter1()); // 3
let counter2 = makeCounter();
console.log(counter2()); // 1
console.log(counter2()); // 2
This code creates a makeCounter
function that returns another function. This inner function uses a closure to access and manipulate the count
variable in the outer function. Each time the inner function is called, it increments the count and returns the new value.
The example code also demonstrates how multiple instances of the counter can be created by calling makeCounter
multiple times. Each instance has its own separate count
variable, so they operate independently.