Skip to content

Instantly share code, notes, and snippets.

@ugultopu
Last active October 30, 2020 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugultopu/15abb1790b3d08384a372cdef3ab5b81 to your computer and use it in GitHub Desktop.
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
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