Skip to content

Instantly share code, notes, and snippets.

@burdiuz
Last active Oct 6, 2018
Embed
What would you like to do?
getClass() - Simple function wrapper over `Object.getPrototypeOf()`.

getClass()

Simple function wrapper over Object.getPrototypeOf().
Additionally provides functions to getClassName() and getParentClass().

Demo on jsFiddle.

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const getProtoConstructor = (value) => {
const proto = Object.getPrototypeOf(value);
if (proto && typeof proto === 'object') {
return proto.constructor;
}
return proto || Object;
}
const getClass = (value) => {
if(value === null || value === undefined) {
return undefined;
}
const constructor = value.constructor;
if(
typeof constructor === 'function'
&& value instanceof constructor
) {
return value.constructor;
}
return getProtoConstructor(value);
};
const getParentClass = (value) => getProtoConstructor(getClass(value));
const getClassName = (value) => {
if (!value) return '';
const def = getClass(value);
return def ? def.name : '';
};
exports.getClassName = getClassName;
exports.getParentClass = getParentClass;
exports.getClass = getClass;
exports.default = getClass;
(function(global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
? factory(exports)
: typeof define === 'function' && define.amd
? define(['exports'], factory)
: factory((global.GetClass = {}));
})(this, function(exports) {
'use strict';
const getProtoConstructor = (value) => {
const proto = Object.getPrototypeOf(value);
if (proto && typeof proto === 'object') {
return proto.constructor;
}
return proto || Object;
}
const getClass = (value) => {
if (value === null || value === undefined) {
return undefined;
}
const constructor = value.constructor;
if (typeof constructor === 'function' && value instanceof constructor) {
return value.constructor;
}
return getProtoConstructor(value);
};
const getParentClass = (value) => getProtoConstructor(getClass(value));
const getClassName = (value) => {
if (!value) return '';
const def = getClass(value);
return def ? def.name : '';
};
exports.getClassName = getClassName;
exports.getParentClass = getParentClass;
exports.getClass = getClass;
exports.default = getClass;
Object.defineProperty(exports, '__esModule', { value: true });
});
{
"author": {
"name": "Oleg Galaburda",
"email": "burdiuz@gmail.com",
"url": "http://actualwave.com/"
},
"bugs": {
"url": "https://gist.github.com/burdiuz/4d3a39f8e8e926a226a210d5beab4592",
"email": "burdiuz@gmail.com"
},
"description": "Returns object constructor",
"homepage": "https://gist.github.com/burdiuz/4d3a39f8e8e926a226a210d5beab4592",
"keywords": [
"js",
"javascript",
"Object",
"constructor",
"class",
"getClass"
],
"license": "MIT",
"main": "get-class.js",
"name": "@actualwave/get-class",
"version": "0.0.6"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment