Skip to content

Latest commit

 

History

History
39 lines (26 loc) · 937 Bytes

File metadata and controls

39 lines (26 loc) · 937 Bytes

Prototypal Inheritance

All objects have prototypes, which describe where to look for properties.

const dev = {type: 'dev'}
dev.type

This creates an object with a property called type. What if you want to make a lot of objects that all share the type property?

const alex = {name: 'alex'}
alex.__proto__ = dev
alex.type

That __proto__ property is only in new versions of JavaScript. The class keyword also makes declaring this kind of inheritance easy.

The older way is to use the Object global.

const morgan = Object.create(dev, {name: 'morgan'})
morgan.type

Constructor Functions

function Foo() {}

const foo = new Foo()

foo instanceof Foo