I read the epub version of the book and have divided my highlights up by chapter since page numbers don’t work well for epubs. Please remember these are highlights, and don’t make a lot of sense without the context of the book in most cases, so you should read the whole book if you have any questions.
These two attributes handily solve all our problems with blocking requests and timing, save for one small catch: while
deferhas been around for a long time, it was only recently standardized, and
asyncis brand new, so we can’t guarantee they’ll be available in all browsers.
We can use this to get information about the current state of elements on the page, check the output of scripts, or even add functionality to the page for the sake of testing. Right now, we can use it to try out new methods and get immediate feedback.
It takes a while to get the hang of where semicolons are absolutely necessary and where ASI can fill in the blanks, so we’re better off erring on the side of caution.
Well-commented code serves as a roadmap for other developers, and helps them understand what decisions you made and why.
The whole idea, after all, is that variables can represent any number of values in a predictable, easy-to-reference package.
…always adhere to the existing code conventions of a project, rather than mixing and matching. On a brand-new project, use whichever syntax you find the most comfortable, but keep an open mind—we have trickier problems to solve than fighting over personal preferences.
It does make a good case for an editor with syntax highlighting, though, which can help you avoid mysterious-seeming errors when assigning an identifier to a variable.
At the same time, we should avoid identifiers that describe their potential values in too much detail, since we may not always be able to immediately predict the values a variable will contain. A variable originally named
milesmay need to contain a value in kilometers one day—confusing for the developers who end up maintaining that code, ourselves included.
distanceworks much better.
There are two kinds of variable scope: local and global. A variable defined outside of a function is global. And because global variables are, well, global, they can be accessed anywhere in the entire application.
A variable defined inside a function can be either local or global, depending on how we define it—which really comes down to whether we declare it by using the keyword
…it’s a good idea to always define your variables using
var. Always using
varmeans local variables stay local and global variables stay global—which means we don’t spend hours of debugging time trying to track down the function that unexpectedly changed a global variable’s value.
When we reference a position within an array using an index, it isn’t much different from working with variables: any reference to an array position takes on the data type of the data it contains—and just like a variable, we can reassign data to a given array position using a single equals sign.
Likewise, we can use either the bracket syntax or the
new Array()syntax to initialize an array with no defined items, just like we can initialize a variable but leave it undefined.
Unless you need to get clever, though, dot notation is the simpler of the two syntaxes, and I find it much easier to read at a glance.
…a function is an object that does something, rather than just holding a value.
One of the more common—and powerful—uses of functions is to provide you with a packaged, reusable method of calculating something. I don’t mean that in a strictly mathematical sense, though you can certainly do that as well. By setting a function up to “return” a value, we allow a function to be treated the same way as we would treat a variable: as a container for data that behaves just like the data it contains.
Control flow statements are used to control what portions of our code are run at a given time, and whether they’re executed at all.
For our purposes, control flow statements fit pretty neatly into two categories: conditional statements and loops.
Conditional statements are a type of control flow concerned with logic: they determine when and where to execute code, based on conditions you specify.
For whoever ends up maintaining our code after us—and for our own sanity—it’s a good idea to keep your scripts as terse as possible. You’ll frequently see this concept referred to as DRY, which stands for don’t repeat yourself. If you have to change something in your code later, you’re better off only needing to do so in one place.
One of the ways I keep the complexity of my own code in check is stepping through it in plain English.
When we use the logical NOT operator (!) in front of another data type—like a number or a string—it reverses the truthy/falsy value of that data.
…we can walk away from this chapter knowing that there’s some way to express whatever conditional logic we might need—and if you can’t remember the exact syntax off the top of your head, well, this chapter isn’t going anywhere.
The bigger problem is that
inhas a catch that a regular
forloop doesn’t: since pretty much everything is an object—and you can add properties to any object—that means
incan end up iterating over properties we never meant for it to know about.
We can add methods and properties to all objects of a certain type by making changes to the
prototypeproperty of a constructor directly—we just can’t change the properties that have already been defined. This works the way you might expect: making additions to
String.prototypeworks the way you’d add properties on an object you created yourself.
window.documentisn’t just a representation of the page; it also provides us with a smarter API for accessing that information.
If we don’t do it responsibly, though, we’ve done something far worse than simply presenting the user with a misaligned
For those users, progressive enhancement meant the difference between finding the information they needed right away, or being forced to keep searching for it—between knowing and not knowing.
What makes a developer is a curiosity, a willingness to learn, and maybe the drive to solve a puzzle or two.