Chapter 13. Creating Custom JavaScript Objects

Previous chapters made use of existing objects, defined as part of the JavaScript language or inherited as part of the browser Document Object Model (DOM). This chapter focuses on how you can create your own custom JavaScript objects and package them for reuse across many applications.

The JavaScript Object and Prototyping

An object in JavaScript is a complex construct usually consisting of a constructor as well as zero or more methods and/or properties. Additionally, all objects in JavaScript derive functionality from the standard JavaScript Object.

The Object itself is not particularly interesting. What Object does is provide the framework for creating new objects. Unlike in other programming languages, though, the JavaScript Object doesn’t provide framework functionality to new objects via traditional object-oriented inheritance and the concept of classes. Instead, JavaScript derives its object functionality from a concept called prototyping.


In a language such as Java or C++, to create a class as an extension of another, you define the class in such a way that it inherits the functionality of the higher-level object and overrides whatever functionality you don’t want.

JavaScript, on the other hand, provides for a constructor, via Object, that allows you to create new objects. It is the Object constructor that allocates the memory for the object, including all of its properties. The Object also provides a prototype property ...

Get Learning JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.