📜  elequent javascript (1)

📅  最后修改于: 2023-12-03 15:00:35.442000             🧑  作者: Mango

Eloquent JavaScript

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:

  • Values, variables, and control flow
  • Functions and closures
  • Objects and arrays
  • Regular expressions
  • Debugging and error handling
  • Async programming with callbacks and promises
  • Modules and code organization
  • Browser and Node.js environments
  • Web programming with HTML, CSS, and DOM manipulation
  • Graphics and animation with Canvas
  • Node.js servers and databases with SQL and MongoDB

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.

Example Code

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.