JavaScriptIntermediate#prototypes#oop

What is prototypal inheritance?

Every object has an internal link to a prototype object. Property lookups walk that prototype chain until found or until null, which is how methods are shared between instances.

Example
function Animal(name){ this.name = name; }
Animal.prototype.speak = function(){ return this.name + ' speaks'; };
const d = new Animal('Rex');
d.speak();                        // 'Rex speaks'
Object.getPrototypeOf(d) === Animal.prototype; // true

Related Questions

1
JavaScriptIntermediate#oop#es6

What is the difference between an ES6 class and a constructor function?

Open
2
JavaScriptAdvanced#event-loop#async

Explain the event loop.

Open
3
JavaScriptAdvanced#event-loop#async

What are microtasks and macrotasks?

Open