Classes
class is cleaner syntax for constructor functions and prototypes. Under the hood, JS is still prototype-based.
- constructor runs on new.
- methods live on the prototype.
- extends creates a subclass.
class Counter {
constructor() {
this.n = 0;
}
inc() {
this.n += 1;
return this.n;
}
}
console.log(new Counter().inc());
After you read this chapter, use Write (when available) and Quiz on this page to lock it in.