![]() ![]() But this approach is rather manual, so ES6 also included several mechanisms (syntax and APIs) for standardized consumption of these iterators. With the ES6 iteration protocol in place, it's workable to consume a data source one value at a time, checking after each next() call for done to be true to stop the iteration. The protocol defines a next() method whose return is an object called an iterator result the object has value and done properties, where done is a boolean that is false until the iteration over the underlying data source is complete. The importance of the iterator pattern is in adhering to a standard way of processing data iteratively, which creates cleaner and easier to understand code, as opposed to having every data structure/source define its own custom way of handling its data.Īfter many years of various JS community efforts around mutually agreed-upon iteration techniques, ES6 standardized a specific protocol for the iterator pattern directly in the language. You don't always know how many pieces of data that you will need to iterate through, so the pattern typically indicates completion by some special value or exception once you iterate through the entire set and go past the end. Calling next() returns the next piece of data (i.e., a "record" or "row" from a database query). The iterator pattern defines a data structure called an "iterator" that has a reference to an underlying data source (like the query result rows), which exposes a method like next(). If this query had only one or a couple of rows, you could handle the entire result set at once, and assign each row to a local variable, and perform whatever operations on that data that were appropriate.īut if the query has 100 or 1,000 (or more!) rows, you'll need iterative processing to deal with this data (typically, a loop). Imagine a data structure that represents a relational database SELECT query, which typically organizes the results as rows. The idea is that it's more common and helpful to iterate the data source-to progressively handle the collection of data by processing the first part, then the next, and so on, rather than handling the entire set all at once. The iterator pattern has been around for decades, and suggests a "standardized" approach to consuming data from a source one chunk at a time. ![]() Since programs are essentially built to process data (and make decisions on that data), the patterns used to step through the data have a big impact on the program's readability. That's OK, because there's a whole book series ahead of you to keep exploring! Iteration Even still, you'll probably finish this chapter with remaining questions. As I've said a dozen times already, take your time. However, this material is still not an exhaustive exposition of the language that's what the rest of the book series is for! Our goal here is still just to get started, and become more comfortable with, the feel of JS, how it ebbs and flows.ĭon't run so quickly through this material that you get lost in the weeds. This chapter should begin to answer some of the "Why?" questions that may be cropping up as you explore JS. ![]() My goal is to help you appreciate the core of how JS works, what makes it tick. ![]() In this chapter, our attention shifts to some of the lower-level root characteristics of JS that underpin virtually every line of code we write.īe aware: this chapter digs much deeper than you're likely used to thinking about a programming language. In Chapter 2, we surveyed syntax, patterns, and behaviors at a high level. If you skipped/skimmed them (especially Chapter 2), I recommend going back to spend some more time with that material. If you've read Chapters 1 and 2, and taken the time to digest and percolate, you're hopefully starting to get JS a little more. You Don't Know JS Yet: Get Started - 2nd Edition Chapter 3: Digging to the Roots of JS ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |