As they advance through their careers, many JavaScript coders like Bob and Ann reach the point where they’re actively using the vast number of elements that form the language. In many cases, however, those skills may not be taken beyond fundamental levels. Our guess is that this is often because JavaScript, using a C-like syntax, bears a surface resemblance to other widespread C-like languages (such as C# and Java), and thus leaves the impression of familiarity.
People often feel that if they know C# or Java, they already have a pretty solid understanding of how JavaScript works. But it’s a trap! When compared to other mainstream languages, JavaScript is much more functionally oriented. Some JavaScript concepts differ fundamentally from those of most other languages.
These differences include the following:
■ Functions are first-class objects—In JavaScript, functions coexist with, and can be treated like, any other JavaScript object. They can be created through literals, referenced by variables, passed around as function arguments, and even returned as function return values. We devote much of chapter 3 to exploring some of the wonderful benefits that functions as first-class objects bring to our JavaScript code.
■ Function closures—The concept of function closures is generally poorly understood,but at the same time it fundamentally and irrevocably exemplifies the importance of functions to JavaScript. For now, it’s enough to know that a function is a closure when it actively maintains (“closes over”) the external variables used in its body. Don’t worry for now if you don’t see the many benefits of closures; we’ll make sure all is crystal clear in chapter 5. In addition to closures, we’ll thoroughly explore the many aspects of functions themselves in chapters 3 and 4, as well as identifier scopes in chapter 5.
■ Scopes—Until recently, JavaScript didn’t have block-level variables (as in other C-like languages); instead, we had to rely only on global variables and function-level variables.
■ Prototype-based object orientation—Unlike other mainstream programming languages (such as C#, Java, and Ruby), which use class-based object orientation, JavaScript uses prototypes. Often, when developers come to JavaScript from class-based languages (such as Java), they try to use JavaScript as if it were Java, essentially writing Java’s class-based code using the syntax of JavaScript. Then, for some reason, they’re surprised when the results differ from what they expect. This is why we’ll go deep into prototypes, how prototype-based object-orientation works, and how it’s implemented in JavaScript.
JavaScript consists of a close relationship between objects and prototypes, and functions and closures. Understanding the strong relationships between these concepts can vastly improve your JavaScript programming ability, giving you a strong foundation for any type of application development, regardless of whether your JavaScript code will be executed in a web page, in a desktop app, in a mobile app, or on the server.
In addition to these fundamental concepts, other JavaScript features can help you write more elegant and more efficient code. Some of these are features that seasoned developers like Bob will recognize from other languages, such as Java and C++. In particular, we focus on the following:
■ Generators, which are functions that can generate multiple values on a per-request basis and can suspend their execution between requests
■ Promises, which give us better control over asynchronous code
■ Proxies, which allow us to control access to certain objects
■ Advanced array methods, which make array-handling code much more elegant
■ Maps, which we can use to create dictionary collections; and sets, which allow us to deal with collections of unique items
■ Regular expressions, which let us simplify what would otherwise be complicated pieces of code
■ Modules, which we can use to break code into smaller, relatively self-contained pieces that make projects more manageable Having a deep understanding of the fundamentals and learning how to use advanced language features to their best advantage can elevate your code to higher levels. Honing your skills to tie these concepts and features together will give you a level of understanding that puts the creation of any type of JavaScript application within your reach.
Next Blog: How will JavaScript evolve?