Created
June 19, 2019 11:55
-
-
Save jonathantneal/89da6772ee8e668711badcee1b86732e to your computer and use it in GitHub Desktop.
The getPropertyDescriptor method returns a property descriptor of a given object
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
/** | |
* 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