Last active
October 30, 2020 03:08
-
-
Save ugultopu/15abb1790b3d08384a372cdef3ab5b81 to your computer and use it in GitHub Desktop.
Prints all (enumerable and non-enumerable) properties of an object by traversing the prototype chain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getAllProperties(object) { | |
// Same as: | |
// if (object === null || object === undefined) return; | |
if (object == null) return; | |
const properties = {...Object.getOwnPropertyDescriptors(object)}; | |
let currentObject = properties; | |
while (object = Object.getPrototypeOf(object)) { | |
currentObject.PROTO = Object.getOwnPropertyDescriptors(object); | |
currentObject = currentObject.PROTO; | |
} | |
return properties; | |
} | |
function f() { | |
this.a = 1; | |
this.b = 2; | |
} | |
require("util").inspect.defaultOptions.depth = null; | |
console.log(getAllProperties(new f())); | |
/** | |
* More information: | |
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties | |
* https://stackoverflow.com/questions/8024149 | |
* https://stackoverflow.com/a/41882441 | |
* https://stackoverflow.com/a/33212776 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment