By Marijn Haverbeke January 15, 2020 ⋅ 9 min read ⋅ Textbooks
Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems.
Jamie Zawinski
Chapter 0: Introduction
DISCLAIMER: These notes are from a senior software engineering student so basic concepts have been skipped and I’ve mostly noted the special syntax/features of JS. These notes are not for beginner programmers.
Functions can act like values and be stored by variables.
Function declaration syntax
functionsquare(x){return x * x;}
Function declarations are not part of the regular top-to-bottom flow of control. They are conceptually moved to the top of their scope and can be used by all the code in that scope.
Arrow function syntax
constsquare=(x)=>{return x * x;};constsquare=x=> x * x;
The arrow comes after the parameters and is followed by the function’s body.
There’s no deep reason to have both arrow functions and the function keyword in the language.
Closure: being able to reference a specific instance of a local binding in an enclosing scope.
Always start by writing something that’s correct and easy to understand.
If you’re worried that it’s too slow - which it usually isn’t since most code simply isn’t executed often enough to take any significant amount of time - you can measure afterward and improve it if necessary
A useful principle is to not add cleverness unless you are absolutely sure you’re going to need it.
Functions can be roughly divided into those that are called for their side effects and those that are called for their return value.
Side effect such as printing the right value.
Return value as in returning some value.
Chapter 4: Data Structures: Objects and Arrays
JavaScript has no “deep” comparison operator to compare object contents (but you can write a custom one).
Arrays have an includes method that checks whether a given value exists in the array.
Foreach loop in JS
for(let element of array){}
Use push and pop to append and remove from the end of an array.
Use unshift and shift to append and remove from the start of an array.
Add three dots before a function parameter for it to be the rest parameter
functionmax(...numbers){}
Rest parameter: a parameter of an array containing all further arguments (similar to *args and **kwargs in Python).
If we know the inputs into a function, we can bind those inputs to specific variable names.
Skimmed because I’ve done regex before but I did the exercises.
Chapter 10: Modules
Module: a piece of program that specifies which other pieces it relies on and which functionality it provides.
Package: a chunk of code that can be distributed.
NPM: a place to store, find, and manage packages.
Use the require() function to load a module via CommonJS.
Use import and export to load a module via ES modules.
Fetching a big file tends to be faster than fetching a lot of smaller files.
Bundler: a tool that bundles a program into a single big file.
Minifier: a tool that minimizes a program by removing comments and whitespace, renaming binding, and replacing pieces of code with equivalent, shorter code.
Be aware that the JavaScript code you run is often not the code as it was written.
Chapter 11: Asynchronous Programming
Synchronous: things happen one at a time.
Asynchronous: multiple things happen at the same time.
One approach to asynchronous programming is to make functions that are slow to take an extra argument called a callback function.
Callback function: when the function finishes, call the callback function with the result.
Asynchronicity is contagious; any function that calls a function that works asynchronously must itself be asynchronous.
Another way of performing asynchronous programming, instead of using a callback function, is to return an object that represents the future event.
Promise: an asynchronous action that may complete at some point and produce a value.
Callback functions and promises are different solutions to the same problem, the problem of knowing when an async function call is done.
However, callback functions perform a function while promises return an object.
Create a promise using the Promise.resolve() function and call .then() on the promise object to get the its result.
E.g.
let fifteen = Promise.resolve(15);
fifteen.then(value=> console.log(`Got ${value}`));// → Got 15
You can add multiple callbacks to a single promise and they will be called, even if you add them after the promise has already resolved (finished).
Also, the then() function returns another promise when its function argument returns.
Think of promises as a way to move values into the future. A normal value is simply there. A promised value might be there or it might be there in the future.
Promise-based functions look similar to regular ones but they differ in that the output may not be available yet.
Handling errors in async programming is a nightmare because it isn’t easy to trace the error.
For callback functions, it’s common to use the first argument to indicate whether the action failed and to use the second argument to hold the value when the action is successful.
For promises, the then() function is only called when the promise is successful and exceptions are propagated. The catch() function is only called when the promise is unsuccessful and is useful in exception handling.
To build an async loop for retries, we need to use a recursive function because regular loops don’t allow us to stop and wait for an async action.
Promises are better than callbacks because they automatically handle reporting and routing of exceptions.
Use the Promise.all() function to return a promise when all promises in an array are resolved.
You can write pseudo-synchronous code to describe an async computation using async and await.
Async function: a function that implicitly returns a promise and that can await other promises in its function body.
E.g.
asyncfunctionfindInStorage(){let local =awaitstorage();if(local !=null)return local;thrownewError('Not found');}
Use await in the function body to pause the execution and wait for the promise to resolve.
Using async and promises together make the code easier to read and write.
JS also has generators like Python which return an iterator.
Create a generator function using the function* keyword.
E.g.
function*powers(n){for(let current = n;; current *= n){yield current;}}for(let power ofpowers(3)){if(power >50)break;
console.log(power);}// → 3// → 9// → 27
An async function is a special type of generator. It produces a promise when called, which is resolved when it returns (finishes) and rejected when it throws an exception.
Event loop: the big loop around your program that only runs one program at a time.
E.g.
Promise.resolve("Done").then(console.log);
console.log("Me first!");// → Me first!// → Done
Computing new values is less error-prone than changing existing values.
Chapter 12: Project: A Programming Language
Chapter skipped.
Part 2: Browser
Chapter 13: JavaScript and the Browser
Network protocol: describes a style of communication over a network.
Hypertext Transfer Protocol (HTTP): a protocol for retrieving named resources.
World Wide Web (WWW): a set of protocols and formats for visiting web pages in a browser.
Uniform Resource Locator (URL): the name of each document on the web.
http://
eloquentjavascript.net/
13_browser.html
protocol
server
path
Hypertext Markup Language (HTML): the document format used for web pages.
To write “<” and “>” in HTML, use > and <.
Use the src attribute of the <script> tag to link to a JS file.
E.g.
<scriptsrc="code/hello.js"></script>
Chapter 14: The Document Object Model
Document Object Model (DOM): representing a webpage as a series of nested boxes.
For the DOM, document.documentElement is the root element.
DOM nodes contains various links to other nearby nodes.
The childNodes array isn’t a real array so you can’t loop over it with a forEach loop. You have to use the index.
Handlers: functions that register and manage an event.
E.g. of handler
<p>Click this document to activate the handler.</p><script>
window.addEventListener("click",event=>{
console.log("You knocked?");
console.log(event);});</script>
Event listeners are called only when the event happens in the context of the object they are registered on.
Event object: objects passed to event handler functions that hold info about the event.
Events are said to propagate from children to parents all the way to the root node.
To stop the propagation, use the event.stopPropagation() function.
Access process.argv to access the command line arguments.
Create a package.json file to hold the dependencies needed to run a program
NPM uses semantic versioning where every time new functionality is added, the middle number has to be incremented. Every time compatibility is broken, so that existing code that uses the package might not work with the new version, the first number has to be incremented.
A ^ means that the version compatible can be higher.