Nicholas C. Zakas
This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.
2012 - 2014 Nicholas C. Zakas
Introduction
Most developers associate object-oriented programming with languages that are typically taught in school, like C++ and Java, which base object-oriented programming around classes. Before you can do anything in these languages, you need to create a class, even if youre just writing a simple command-line program. Common design patterns in the industry reinforce class-based concepts as well. But JavaScript doesnt use classes, and this is part of the reason people get confused when they try learning it after C++ or Java. Object-oriented languages have several characteristics:
- Encapsulation - Data can be grouped together with functionality that operates on that data. This, quite simply, is the definition of an object.
- Aggregation - One object can reference another object.
- Inheritance - A newly created object has the same characteristics as another object without explicitly duplicating its functionality.
- Polymorphism One interface may be implemented by multiple objects.
JavaScript has all these characteristics, though because the language has no concept of classes, some arent implemented in quite the way you might expect. At first glance, a JavaScript program might even look like a procedural program you would write in C. If you can write a function and pass it some variables, you have a working script that seemingly has no objects. A closer look at the language, however, reveals the existence of objects through the use of dot notation.
Many object-oriented languages use dot notation to access properties and methods on objects, and JavaScript is syntactically the same. But in JavaScript, you never need to write a class definition, import a package, or include a header file. You just start coding with the data types that you want, and you can group those together in any number of ways. You could certainly write JavaScript in a procedural way, but its true power emerges when you take advantage of its object-oriented nature. Thats what this book is about.
Make no mistake: A lot of the concepts you may have learned in more traditional object-oriented programming languages dont necessarily apply to JavaScript. While that often confuses beginners, as you read, youll quickly find that JavaScripts weakly typed nature allows you to write less code to accomplish the same tasks as other languages. You can just start coding without planning the classes that you need ahead of time. Need an object with specific fields? Just create an ad hoc object wherever you want. Did you forget to add a method to that object? No problem - just add it later.
Inside these pages, youll learn the unique way that JavaScript approaches object-oriented programming. Leave behind the notions of classes and class-based inheritance and learn about prototype-based inheritance and constructor functions that behave similarly. Youll learn how to create objects, define your own types, use inheritance, and otherwise manipulate objects to get the most out of them. In short, youll learn everything you need to know to understand and write JavaScript professionally. Enjoy!
Who This Book Is for
This book is intended as a guide for those who already understand object-oriented programming but want to know exactly how the concept works in JavaScript. Familiarity with Java, C#, or object-oriented programming inother languages is a strong indicator that this book is for you. In particular, this book is aimed at three groups of readers:
- Developers who are familiar with object-oriented programming concepts and want to apply them to JavaScript
- Web application and Node.js developers trying to structure their code more effectively
- Novice JavaScript developers trying to gain a deeper understanding of the language
This book is not for beginners who have never written JavaScript. You will need a good understanding of how to write and execute JavaScript code to follow along.
Overview
Chapter 1: Primitive and Reference Types introduces the two different value types in JavaScript: primitive and reference. Youll learn what distinguishes them from each other and how understanding their differences is important to an overall understanding of JavaScript.
Chapter 2: Functions explains the ins and outs of functions in JavaScript. First-class functions are what makes JavaScript such an interesting language.
Chapter 3: Understanding Objects details the makeup of objects in JavaScript. JavaScript objects behave differently than objects in other languages, so a deep understanding of how objects work is vital to mastering the language.
Chapter 4: Constructors and Prototypes expands on the previous discussion of functions by looking more specifically at constructors. All constructors are functions, but they are used a little bit differently. This chapter explores the differences while also talking about creating your own custom types.
Chapter 5: Inheritance explains how inheritance is accomplished in JavaScript. Though there are no classes in JavaScript, that doesnt mean inheritance isnt possible. In this chapter, youll learn about prototypal inheritance and how it differs from class-based inheritance.
Chapter 6: Object Patterns walks through common object patterns. There are many different ways to build and compose objects in JavaScript, and this chapter introduces you to the most popular patterns for doing so.
Acknowledgments
Id like to thank Kate Matsudaira for convincing me that self-publishing an ebook was the best way to get this information out. Without her advice, Id probably still be trying to figure out what I should do with the information contained in this book.
Thanks to Rob Friesel for once again providing excellent feedback on an early copy of this book, and Cody Lindley for his suggestions. Additional thanks to Angus Croll for his technical review of the finished version his nitpicking made this book much better.
Thanks as well to Bill Pollock, whom I met at a conference and who started the ball rolling on publishing this book with No Starch Press.
Help and Support
If you have questions, comments, or other feedback about this book, please visit the mailing list at: http://groups.google.com/group/zakasbooks.
Chapter 1: Primitive and Reference Types
Most developers learn object-oriented programming by working with class-based languages such as Java or C#. When these developers start learning JavaScript, they get disoriented because JavaScript has no formal support for classes. Instead of defining classes from the beginning, with JavaScript you can just write code and create data structures as you need them. Because it lacks classes, JavaScript also lacks class groupings such as packages. Whereas in languages like Java, package and class names define both the types of objects you use and the layout of files and folders in your project, programming in JavaScript is like starting with a blank slate: You can organize things any way you want. Some developers choose to mimic structures from other languages, while others take advantage of JavaScripts flexibility to come up with something completely new. To the uninitiated, this freedom of choice can be overwhelming, but once you get used to it, youll find JavaScript to be an incredibly flexible language that can adapt to your preferences quite easily.