liveBook · Manning

Class

As explained in The Joy of JavaScript

A class is a programming-language construct that defines a type’s structure or behavior and provides a basis for creating or representing objects.

A JavaScript class provides a familiar way to define objects and model inheritance. It streamlines prototype configuration, especially for domain models, without replacing JavaScript’s prototype-based object system. section 2.3 section 2.4

Unlike class-based models that copy inherited properties into instances, JavaScript links objects through prototypes. Classes make inheritance hierarchies easier to express, but can obscure how those links work. section 2.3 section 2.4

Choose classes with their tradeoffs in mind:

  • They reduce the boilerplate of constructor functions and prototype setup. section 2.3
  • They are useful for inheritance-based modeling, but are not the only way to build objects or practice object-oriented programming. section 2.3 section 2.4
class Transaction {  
 constructor(sender, recipient, funds = 0.0) {
   this.sender = sender;
   this.recipient = recipient;
   this.funds = Number(funds);
 }
 displayTransaction() {
     return `Transaction from ${this.sender} to ${this.recipient}               
        for ${this.funds}`;
  }
}

A Transaction class uses a constructor to initialize instance data and a method to format a transaction. section 2.3

How The Joy of JavaScript covers it

How deep it goes
Explains prototype linking versus copying and compares class syntax with constructor functions, while emphasizing that classes can obscure prototype mechanics.
Examples
Uses Transaction and HashTransaction examples, and contrasts a Transaction class with a module-pattern implementation.
What you can do after
Evaluate when classes simplify inheritance-based modeling and recognize how JavaScript prototypes differ from class-based copying.
How it connects
Connects classes to prototypes, constructor functions, inheritance, and the module pattern.

Also explained in

Compare how 21 books explain class →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.