JS: Show Prototype Chain 📜
Here is a function that returns any object's Prototype Chain as array.
/* xah_get_proto_chain(obj) Take any object, returns a array, the order of elements shows the proto chain. First element is obj itself, last element is root. http://xahlee.info/js/js_show_prototype_chain.html Version: 2020-06-25 */ const xah_get_proto_chain = (x) => { const result = [x]; let t = x; while ((t = Reflect.getPrototypeOf(t)) !== null) result.push(t); return result; }; // test const x1 = { k: 1 }; const x2 = Object.create(x1); x2.k = 2; const x3 = Object.create(x2); x3.k = 3; console.log(xah_get_proto_chain(x3)); // [ { k: 3 }, { k: 2 }, { k: 1 }, [Object: null prototype] {} ] // s------------------------------ // standard builtin objects console.log(xah_get_proto_chain([3, 4])); // [ [ 3, 4 ], Object(0) [], [Object: null prototype] {} ] console.log(xah_get_proto_chain({ p: 1 })); // [ { p: 1 }, [Object: null prototype] {} ] console.log(xah_get_proto_chain(function f() { return 3; })); // [ [Function: f], {}, [Object: null prototype] {} ] console.log(xah_get_proto_chain(new Date())); // [ 2026-02-03T17:35:39.302Z, {}, [Object: null prototype] {} ] // note, nodejs prints Object.prototype as {}, same as a newly created object, even though they are different object console.log(Object.prototype); // [Object: null prototype] {} // similarly, Array.prototype prints as [], etc.
JavaScript. Object and Inheritance
- JS: Object (basics)
- JS: Object Overview
- JS: Object Type
- JS: Test is Object Type 📜
- JS: Find Object's sub-type
- JS: Prototype and Inheritance
- JS: Prototype Chain
- JS: Object.prototype.isPrototypeOf
- JS: Get Set Prototype
- JS: Show Prototype Chain 📜
- JS: Prototype Tree
- JS: Dot Notation and Prototype Chain
- JS: Create Object
- JS: Object Literal Expression
- JS: Object.create
- JS: Object Literal Expression vs Object.Create
- JS: Create Object with Parent X
- JS: Prevent Adding Property
- JS: Deep Copy Array or Object 📜
- JS: Test Equality of Array and Object by Content 📜
- JS: Add Method to Prototype
- JS: Object (class)
- JS: Object Constructor
- JS: Object.prototype