Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created June 19, 2019 11:55
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 jonathantneal/89da6772ee8e668711badcee1b86732e to your computer and use it in GitHub Desktop.
Save jonathantneal/89da6772ee8e668711badcee1b86732e to your computer and use it in GitHub Desktop.
The getPropertyDescriptor method returns a property descriptor of a given object
/**
* Return the property descriptor of a given object
* @param {Object} obj - The object for which to access property descriptors
* @param {String|Symbol} key - The name or Symbol of the property whose descriptor is to be retrieved
* @return {Object} The property descriptor of the given object
*/
function getPropertyDescriptor (obj, key) {
if (obj === undefined || obj === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
if (key in obj) {
return getRecursivePropertyDescriptor(obj);
}
function getRecursivePropertyDescriptor (obj) {
return Object.prototype.hasOwnProperty.call(obj, key)
? Object.getOwnPropertyDescriptor(obj, key)
: getRecursivePropertyDescriptor(Object.getPrototypeOf(obj));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment