4Objects and Prototypes
JavaScript distinguishes itself from most usual “object-oriented programming” (OOP) languages, which are “class based”: JavaScript is “prototype based”. This originality is confusing for people accustomed to other OOP languages, and sometimes perceived as a weakness vis-à-vis better “controlled” languages such as Java, of which it “inherited” the name based on a misunderstanding.
4.1. Introduction
This chapter is organized as follows:
- – an introduction about the notions of “concept” versus “named entities” should help in understanding the difference between “class-based” and “prototype-based” OOP;
- – section 4.2: This section details the syntax and use of the “object literal” in JavaScript, popular through the JSON format, which is widely used to exchange data over the Internet;
- – section 4.3: This section deals with the methods of the built-in objects “Object” and “Object.prototype”, how they provide the notion of inheritance by delegation and how objects can be related;
- – section 4.4: After analyzing the role of prototypes, we detail the three approaches for creating objects:
- - literal: this includes the operator {..}, the role of the functions Object and Function, to enlighten the role of the “prototypes” in the inheritance mechanism in JavaScript,
- - prototypal: this includes the innovative methods Object.create and Object.assign (ES6),
- - classical: this consists of the operator “new” and the “constructor” functions.
We can hardly talk about objects ...
Get JavaScript and Open Data 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.