Chapter 7
Objects and Prototypes
In Section 4.4, we learned how to make plain objects, which we used as simple associative arrays of key–value pairs. In this chapter, we’ll make more general versions of JavaScript objects, ones that have both properties (data) and methods (functions) attached to them.
7.1 Defining Objects
There is a dizzying variety of ways to define objects in JavaScript, but we’ll focus on one of the most classic ways, which is to use functions (Chapter 5). The result will be an object constructor function that can be used to create (or instantiate) a new object (called an instance) using the new
syntax we first saw in Section 4.2.
We’ll start by defining a Phrase
object. Eventually, we’ll use Phrase
to represent a phrase ...
Get Learn Enough JavaScript to Be Dangerous: Write Programs, Publish Packages, and Develop Interactive Websites with JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.