Inheritance lets an object access properties it does not own by searching its prototype chain. JavaScript classes provide a familiar syntax for inheritance, but rely on prototypes underneath. section 9.3.1 section 9.0
Keep these mechanics in mind:
- Property lookup checks the object first, then its prototype and successive prototypes until the search ends. Data properties and methods are inherited alike. section 9.3.1
- Classes use extends to form a parent-child relationship. A child constructor can call super before using this; static properties are not inherited, and inheritance is linear. section 9.4.2
A property found up the chain
Give yoshi its own skulk property, then set hattori as its prototype. Yoshi can now access hattori’s sneak property; chaining hattori to kuma makes kuma’s creep property accessible through both objects. section 9.3.1
Listing 9.8 A class that inherits from a parent
class Person {
name: string;
constructor(name: string) {
this.name = name;
}
dance() {
return true;
}
}
class Ninja extends Person { #A
sneakiness: number;
constructor(name: string, sneakiness = 3) { #B
super(name); #B
this.sneakiness = sneakiness; #B
} #B
sneak() {
return `Sneak level ${this.sneakiness}`;
}
}
const ninja = new Ninja("Chizu", 5);
console.assert(
ninja instanceof Ninja,
"A ninja is a ninja",
);
console.assert(
ninja instanceof Person,
"A ninja is also a person",
);
See a Person and Ninja class example using extends, super, and instanceof. section 9.4.2






















