Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript

Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript

English | 2012 | ISBN: 978-0-321-81218-6 | 228 Pages | PDF, EPUB | 18 MB


In order to truly master JavaScript, you need to learn how to work effectively with the language’s flexible, expressive features and how to avoid its pitfalls. No matter how long you’ve been writing JavaScript code, Effective JavaScript will help deepen your understanding of this powerful language, so you can build more predictable, reliable, and maintainable programs.
Author David Herman, with his years of experience on Ecma’s JavaScript standardization committee, illuminates the language’s inner workings as never before—helping you take full advantage of JavaScript’s expressiveness. Reflecting the latest versions of the JavaScript standard, the book offers well-proven techniques and best practices you’ll rely on for years to come.
Effective JavaScript is organized around 68 proven approaches for writing better JavaScript, backed by concrete examples. You’ll learn how to choose the right programming style for each project, manage unanticipated problems, and work more successfully with every facet of JavaScript programming from data structures to concurrency.

+

A flexible wiki library might provide application writers with a choice of markup languages, since many different popular formats have emerged over the years.

To make this work, we need to separate the functionality of extracting the contents of user-created markup source text from the rest of the wiki functionality, such as account management, revision history, and content storage. The rest of the application should interact with the
extraction functionality through an interfacewith a well- documented set of properties and methods. By programming strictly to the interface’s documented API and ignoring the implementation details of those methods, the rest of the application can function correctly regardless of which source format an application chooses to use.

Let’s look a little more closely at what kind of interface is needed for wiki content extraction. The library must be able to extract metadata such as page title and author and to format page contents as HTML for displaying to wiki readers. We can represent each page in the wiki as an object that provides access to this data through page methods such as getTitle,getAuthor, and toHTML.

Next, the library needs to provide a way to create an application with a custom wiki formatter, as well as some built-in formatters for popular markup formats. For example, an application writer might wish to use the MediaWiki format (the format used by Wikipedia):

How would a formatter such as Wiki.formats.MEDIAWIKIbe implemented? Programmers familiar with class-based programming might be inclined to create a base Pageclass that represents the user-created content and implement each different format as a subclass of Page. The MediaWiki format would be implemented with a class MWPagethat extends Page, and MEDIAWIKIwould be a “factory function” that returns an instance of MWPage:

(See Chapter 4 for more about implementing class hierarchies with constructors and prototypes.) But what practical purpose does the base Pageclass serve? Since MWPageneeds its own implementation of the methods required by the wiki application—getTitle, getAuthor, and to HTML—there’s not necessari ly any useful i mplementation code to inherit. Notice, too, that the displayPagemethod above does not care about the inheritance hierarchy of the page object; it only requires the relevant methods in order to work. So implementations of wiki formats are free to implement those methods however they like.

Where many object-oriented languages encourage structuring your programs around classes and inheritance, JavaScript tends not to stand on ceremony. It is often perfectly sufficient to provide an implementation for an interface like the MediaWiki page format with a simple object literal: