JS Built-ins
Prototypesbeginner

Prototypes β€” Class constructor and methods

πŸ“– Usage Example
class Animal {
  constructor(name) { this.name = name; }
  speak() { return this.name + ' makes a sound.'; }
}
const dog = new Animal('Rex');
dog.speak(); // 'Rex makes a sound.'

Classes group related state (properties) and behavior (methods) under one blueprint.

ES6 Class Syntax

ES6 introduced the class keyword, which is syntactic sugar over JavaScript's prototype-based model. A class can have a constructor method that runs when you create an instance, and methods that become part of the prototype.

Challenge: Create a class with a constructor and a method that returns a formatted string.

πŸ’‘ Hints (3)
  • β€’ constructor is called when you use new
  • β€’ this refers to the instance being created
  • β€’ Methods are called on instances