Skip to content

Instantly share code, notes, and snippets.

@burdiuz
Last active October 6, 2018 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burdiuz/4d3a39f8e8e926a226a210d5beab4592 to your computer and use it in GitHub Desktop.
Save burdiuz/4d3a39f8e8e926a226a210d5beab4592 to your computer and use it in GitHub Desktop.
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