Lab 05: Prototype & Classes
Overview
Step 1: Prototype Chain
// Every object has a prototype (except Object.prototype)
const animal = {
breathe() { return `${this.name} breathes`; },
toString() { return `[Animal: ${this.name}]`; }
};
const dog = Object.create(animal);
dog.name = 'Rex';
dog.bark = function() { return `${this.name} barks!`; };
console.log(dog.breathe()); // Rex breathes (inherited)
console.log(dog.bark()); // Rex barks!
console.log(dog.toString()); // [Animal: Rex]
console.log(Object.getPrototypeOf(dog) === animal); // true
// Prototype chain lookup
console.log(dog.hasOwnProperty('name')); // true (own property)
console.log(dog.hasOwnProperty('breathe')); // false (inherited)
console.log('breathe' in dog); // true (searches chain)
// Viewing the chain
let obj = dog;
while (obj) {
console.log(Object.getOwnPropertyNames(obj));
obj = Object.getPrototypeOf(obj);
}Step 2: Object.create and Prototypal Inheritance
Step 3: Class Syntax
Step 4: Private Fields & Methods
Step 5: Inheritance with extends/super
Step 6: Symbol.iterator
Step 7: Mixin Pattern
Step 8: Capstone — Animal Kingdom
Summary
Feature
Syntax
Notes
Last updated
