Skip to content

Instantly share code, notes, and snippets.

@allenhwkim
Created September 28, 2018 19:47
Show Gist options
  • Select an option

  • Save allenhwkim/e96fa23b29aa51aa98842432ec4b7d75 to your computer and use it in GitHub Desktop.

Select an option

Save allenhwkim/e96fa23b29aa51aa98842432ec4b7d75 to your computer and use it in GitHub Desktop.
obj.constructor.name polyfill
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