Recap

Sophomore Year, page 11 of 11. A one-minute read.

  • A class is a blueprint for creating objects. Each object made from it is an instance. Class names use PascalCase.
  • The constructor runs automatically when you create an instance with new. Use this to set up properties.
  • Methods are defined inside the class body without commas. Every instance shares them.
  • Getters (get name()) look like properties but are calculated. Setters (set name(v)) run on assignment, great for validation.
  • static methods and properties belong to the class itself, not instances.
  • Private fields (#name) can only be used inside the class. This is encapsulation.
  • extends creates a child class. Call super(...) in the child's constructor, and super.method() to reuse a parent's method. Children can override methods.
  • this depends on how a function is called: method call → the object; new → the new object; alone → undefined; arrow functions → the surrounding this. Fix lost this with an arrow wrapper or bind.
  • Classes are built on prototypes: methods live on ClassName.prototype and are shared by all instances through the prototype chain.

Reading is step one

CurlyScript turns these notes into a game: short questions, an explanation after every answer, and a test at the end of each grade. Free, with nothing to sign up for.

Start learning