Last active
February 25, 2016 05:19
-
-
Save tkrkt/4cd932fcbd93f5e962d3 to your computer and use it in GitHub Desktop.
getPropertyDescriptor
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 getPropertyDescriptor(obj, key, path) { | |
path = path || []; | |
path.push(obj); | |
if (obj.hasOwnProperty(key)) { | |
var descriptor = Object.getOwnPropertyDescriptor(obj, key); | |
descriptor.path = path; | |
descriptor.owner = obj; | |
return descriptor; | |
} else if (key in obj){ | |
return getPropertyDescriptor(obj.__proto__, key, path); | |
} else { | |
throw new Error('key: ' + key + ' is not in object'); | |
} | |
} | |
// # usage | |
// | |
// getPropertyDescriptor(document, 'addEventListener'); | |
// => | |
// { | |
// configurable: true, | |
// enumerable: true, | |
// owner: EventTarget, | |
// path: [document, HTMLDocument, Document, Node, EventTarget], | |
// value: addEventListener(), | |
// writable: true | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment