Introduction - The Principles of Object-Oriented Javascript (2014)

The Principles of Object-Oriented Javascript (2014)

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 you’re just writing a simple command-line program.

Common design patterns in the industry reinforce class-based concepts as well. But JavaScript doesn’t 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 aren’t 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. That’s what this book is about.

Make no mistake: A lot of the concepts you may have learned in more traditional object-oriented programming languages don’t necessarily apply to JavaScript. While that often confuses beginners, as you read, you’ll quickly find that JavaScript’s 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, you’ll 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. You’ll learn how to create objects, define your own types, use inheritance, and otherwise manipulate objects to get the most out of them. In short, you’ll 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 in other 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 introduces the two different value types in JavaScript: primitive and reference. You’ll learn what distinguishes them from each other and how understanding their differences is important to an overall understanding of JavaScript.

Chapter 2 explains the ins and outs of functions in JavaScript. First-class functions are what makes JavaScript such an interesting language.

Chapter 3 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 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 explains how inheritance is accomplished in JavaScript. Though there are no classes in JavaScript, that doesn’t mean inheritance isn’t possible. In this chapter, you’ll learn about prototypal inheritance and how it differs from class-based inheritance.

Chapter 6 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.