Last active
December 16, 2015 01:49
-
-
Save h2non/5358438 to your computer and use it in GitHub Desktop.
Simple Function to get inherited prototype Object members
This file contains hidden or 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
// for ES5 compliant engines (tested in Chrome, FF and IE9+) | |
// Under WTFPL license ;) | |
function getObjectInheritedMembers(obj) { | |
var members = {}; | |
if (['boolean', 'number', 'string', 'undefined'].indexOf(typeof obj) !== -1 || obj === null) { // support for primitives types | |
obj = Object(obj); | |
} else if (typeof obj === 'function') { | |
members[obj.name || (obj.toString().match(/function (\w*)/) || obj.toString().match(/\[object (\w*)\]/))[1] || 'Anonymous Function'] = Object.getOwnPropertyNames(obj); | |
obj = obj.prototype; | |
} else if (obj.toString() !== '[object Object]' && obj.prototype) { // fix Objects instances and IE | |
members[obj.prototype.constructor.name || (obj.prototype.constructor.toString().match(/function (\w*)/) || obj.prototype.constructor.toString().match(/\[object (\w*)\]/))[1] || 'Anonymous Function'] = Object.getOwnPropertyNames(obj); | |
obj = obj.prototype; | |
} else if (!Object.getPrototypeOf(obj) && obj.constructor) { | |
members[(obj.constructor.name || (obj.constructor.toString().match(/function (\w*)/) || obj.constructor.toString().match(/\[object (\w*)\]/))[1] || 'Anonymous Function')] = Object.getOwnPropertyNames(obj); | |
} | |
while (obj = Object.getPrototypeOf(obj)) { | |
if (obj && obj.constructor) { | |
members[(obj.constructor.name || (obj.constructor.toString().match(/function (\w*)/) || obj.constructor.toString().match(/\[object (\w*)\]/) || [0, 'Anonymous Function'])[1])] = Object.getOwnPropertyNames(obj); | |
} | |
} | |
return members; | |
} | |
// test | |
getObjectInheritedMembers(HTMLDivElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment