Created
September 28, 2018 19:47
-
-
Save allenhwkim/e96fa23b29aa51aa98842432ec4b7d75 to your computer and use it in GitHub Desktop.
obj.constructor.name polyfill
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
| const getClassName = function(obj) { | |
| if (obj.constructor.name) return obj.constructor.name; | |
| // Chrome "function HTMLButtonElement() { [native code] }" | |
| var funcMatch = obj.constructor.toString().match(/^\s*function\s*(\S+)\s*\(/); | |
| // IE11 "[object HTMLButtonElement]" | |
| var objMatch = obj.constructor.toString().match(/\[object\s(\S+)\]/); | |
| return objMatch ? objMatch[1] : funcMatch[1]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment