Skip to main content

Command Palette

Search for a command to run...

JavaScript Using __proto__ to define prototype chain

Updated
1 min readView as Markdown

This is just about how to define a prototype chain using .prototype and .__proto__.

function animal() {}
animal.prototype.hasFur='has fur'

function person() {}
person.prototype.isMammal='is mammal'

function me() {}
me.prototype.myName='my name is logan'

function foo() {}

// define prototype chain here!!!!
foo.__proto__=me.prototype
me.prototype.__proto__=person.prototype
person.prototype.__proto__=animal.prototype
animal.prototype.__proto__=null

console.log(foo instanceof animal)
console.log(foo.hasFur, foo.isMammal, foo.myName)

Output

"has fur", "is mammal", "my name is logan"

Notice that foo has inherited properties from entities up the prototype chain (like hasFur or isMammal or myName).

Just remember that this.prototype has property __proto__ which is a link to its parent .prototype up the prototype chain. Notice __proto__ of animal.prototype is set to null, which signifies the end of the chain.

More from this blog

Linux and Web

31 posts