Skip to content

Classes, Inheritance, Functional programming

Wei edited this page Mar 20, 2019 · 1 revision

When you try to access a property on the new object, it checks the object’s own properties first. If it doesn’t find it there, it checks the [[Prototype]], and so on up the prototype chain until it gets back to Object.prototype, which is the root delegate for most objects.

class

class Greeter { constructor (name) { this.name = name || 'John Doe'; } hello () { return Hello, my name is ${ this.name }; } }

const george = new Greeter('George');

const msg = george.hello();

console.log(msg); // Hello, my name is George

Factory

const proto = { hello () { return Hello, my name is ${ this.name }; } };

const greeter = (name) => Object.assign(Object.create(proto), { name });

const george = greeter('george');

const msg = george.hello();

console.log(msg);

Functional Inheritance

Functional inheritance makes use of a factory function, and then tacks on new properties using concatenative inheritance.

Clone this wiki locally