get-constructor-name
Version:
Get the Name of a JavaScript Function's Constructor. Returns String or Undefined.
16 lines (15 loc) • 596 B
JavaScript
function getConstructorName(obj) {
try {
if (typeof obj === "object" && typeof obj.constructor === "function" && typeof obj.constructor.name === "string") {
if (obj.constructor.name !== "") {
return obj.constructor.name;
}
}
} catch (error) {
return undefined;
}
}
if (typeof define === "function") define(() => getConstructorName);
if (typeof module === "object") module.exports = getConstructorName;
if (typeof window === "object") window.getConstructorName = getConstructorName;
if (typeof self === "object") self.getConstructorName = getConstructorName;