JS Built-ins
Prototypesintermediate

Prototypes β€” Class properties from constructor

πŸ“– Usage Example
class Point {
  constructor(x, y) { this.x = x; this.y = y; }
}
const p = new Point(3, 4);
p.x; // 3

Properties assigned in the constructor are attached to every instance.

Setting Properties in Constructor

Properties set in the constructor become own properties of each instance. They are not shared across instances and are directly settable and readable.

Challenge: Create a class that assigns multiple properties in the constructor and verify they work correctly.

πŸ’‘ Hints (3)
  • β€’ Use this.propertyName to set properties
  • β€’ Each instance gets its own copy of properties
  • β€’ Properties set in constructor are own properties