Skip to main content

Command Palette

Search for a command to run...

[JavaScript] Object-Oriented Programming (OOP) with JavaScript : Prototypal Inheritance on Built-in Objects (feat. prototype chain)

Published
•View as Markdown
console.log(jonas.__proto__); // prototype of jonas

prototype chain 1.PNG

console.log(jonas.__proto__.__proto__); // top of the prototype chain
console.log(jonas.__proto__.__proto__.__proto__); // null

prototype chain 2.PNG

console.dir(Person.prototype.constructor);

prototype chain 3.PNG

const arr = [3, 6, 4, 5, 6, 9, 3]; // new Array === []
console.log(arr.__proto__);
console.log(arr.__proto__ === Array.prototype);

prototype chain 4.PNG

console.log(arr.__proto__.__proto__);

prototype chain 5.PNG

Array.prototype.unique = function () {
  return [...new Set(this)];
};

console.log(arr.unique());

prototype chain 6.PNG

  • This is not a good way to add a prototype method. Because 1. JavaScript could update the method with the same name, then the bug can occur. 2. other developers in the team can use the same name, then it also can make some bugs.
// check prototype chains and inheritance
const h1 = document.querySelector('h1');
console.dir(x => x + 1);

prototype chain 7.PNG

  • It is a huge prototype chain.

JavaScript

Part 1 of 50

More from this blog

Jayden's Blog

166 posts