Chapter 16. JavaScript Objects
16.0. Introduction
Your JavaScript applications can consist entirely of functions and variables—both local and global—but if you want to ensure ease of reuse, compactness of code, and efficiency, as well as code that plays well with other libraries, you’re going to need to consider opportunities to encapsulate your code into objects.
Luckily, working with objects in JavaScript isn’t much more complicated than working with functions. After all, a JavaScript function is an object, and all objects are, technically, just functions.
Confused yet?
Unlike languages such as Java or C++, which are based on classes and class instances, JavaScript is based on prototypal inheritance. What prototypal inheritance means is that reuse occurs through creating new instances of existing objects, rather than instances of a class. Instead of extensibility occurring through class inheritance, prototypal extensibility happens by enhancing an existing object with new properties and methods.
Prototype-based languages have an advantage in that you don’t have to worry about creating the classes first, and then the applications. You can focus on creating applications, and then deriving the object framework via the effort.
It sounds like a mishmash concept, but hopefully as you walk through the recipes you’ll get a better feel for JavaScript’s prototype-based, object-oriented capabilities.
Note
ECMAScript and all its variations, including JavaScript, isn’t the only prototype-based language. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access