Skip to main content

[JS] Inheritance and the prototype chain

caution

This is not original content, but just a note from articles I read.

Intro

When it comes to inheritance, JavaScript only has one construct: objects.

Each object has a private property which holds a link to another object called its prototype.

What are the differences between __proto__ and prototype?

var b = new Foo(20);
var c = new Foo(30);

img

__proto__ is the actual object that is used in the lookup chain to resolve methods, etc.

prototype is the object that is used to build __proto__ when you create an object with new:

( new Foo ).__proto__ === Foo.prototype
( new Foo ).prototype === undefined

Example

img

Animal.prototype.constructor is itself.

cat.__proto__.constructor is Animal

cat.constructor is Animal

Reference