if (!Object.prototype.getClassName) {Object.prototype.getClassName = function () {return Object.prototype.toString.call(this).match(/^\[object\s(.*)\]$/)[1];}}
var test = [1,2,3,4,5];
alert(test.getClassName()); // returns Array
function getType(o) {return Object.prototype.toString.call(o).match(/^\[object\s(.*)\]$/)[1];}function isInstance(obj, type) {var ret = false,isTypeAString = getType(type) == "String",functionConstructor, i, l, typeArray, context;if (!isTypeAString && getType(type) != "Function") {throw new TypeError("type argument must be a string or function");}if (obj !== undefined && obj !== null && obj.constructor) {//get the Function constructorfunctionConstructor = obj.constructor;while (functionConstructor != functionConstructor.constructor) {functionConstructor = functionConstructor.constructor;}//get the object's windowcontext = functionConstructor == Function ? self : functionConstructor("return window")();//get the constructor for the typeif (isTypeAString) {//type is a string so we'll build the context (window.Array or window.some.Type)for (typeArray = type.split("."), i = 0, l = typeArray.length; i < l && context; i++) {context = context[typeArray[i]];}} else {//type is a function so execute the function passing in the object's window//the return should be a constructorcontext = type(context);}//check if the object is an instance of the constructorif (context) {ret = obj instanceof context;if (!ret && (type == "Number" || type == "String" || type == "Boolean")) {ret = obj.constructor == context}}}return ret;}
function type(obj){return Object.prototype.toString.call(obj]).match(/\s\w+/)[0].trim()}
return [object String] as Stringreturn [object Number] as Numberreturn [object Object] as Objectreturn [object Undefined] as Undefinedreturn [object Function] as Function
/*** Describes the type of a variable.*/class VariableType{type;name;
/*** Creates a new VariableType.** @param {"undefined" | "null" | "boolean" | "number" | "bigint" | "array" | "string" | "symbol" |* "function" | "class" | "object"} type the name of the type* @param {null | string} [name = null] the name of the type (the function or class name)* @throws {RangeError} if neither <code>type</code> or <code>name</code> are set. If <code>type</code>* does not have a name (e.g. "number" or "array") but <code>name</code> is set.*/constructor(type, name = null){switch (type){case "undefined":case "null":case "boolean" :case "number" :case "bigint":case "array":case "string":case "symbol":if (name !== null)throw new RangeError(type + " may not have a name");}this.type = type;this.name = name;}
/*** @return {string} the string representation of this object*/toString(){let result;switch (this.type){case "function":case "class":{result = "a ";break;}case "object":{result = "an ";break;}default:return this.type;}result += this.type;if (this.name !== null)result += " named " + this.name;return result;}}
const functionNamePattern = /^function\s+([^(]+)?\(/;const classNamePattern = /^class(\s+[^{]+)?{/;
/*** Returns the type information of a value.** <ul>* <li>If the input is undefined, returns <code>(type="undefined", name=null)</code>.</li>* <li>If the input is null, returns <code>(type="null", name=null)</code>.</li>* <li>If the input is a primitive boolean, returns <code>(type="boolean", name=null)</code>.</li>* <li>If the input is a primitive number, returns <code>(type="number", name=null)</code>.</li>* <li>If the input is a primitive or wrapper bigint, returns* <code>(type="bigint", name=null)</code>.</li>* <li>If the input is an array, returns <code>(type="array", name=null)</code>.</li>* <li>If the input is a primitive string, returns <code>(type="string", name=null)</code>.</li>* <li>If the input is a primitive symbol, returns <code>(type="symbol", null)</code>.</li>* <li>If the input is a function, returns <code>(type="function", name=the function name)</code>. If the* input is an arrow or anonymous function, its name is <code>null</code>.</li>* <li>If the input is a function, returns <code>(type="function", name=the function name)</code>.</li>* <li>If the input is a class, returns <code>(type="class", name=the name of the class)</code>.* <li>If the input is an object, returns* <code>(type="object", name=the name of the object's class)</code>.* </li>* </ul>** Please note that built-in types (such as <code>Object</code>, <code>String</code> or <code>Number</code>)* may return type <code>function</code> instead of <code>class</code>.** @param {object} value a value* @return {VariableType} <code>value</code>'s type* @see <a href="http://stackoverflow.com/a/332429/14731">http://stackoverflow.com/a/332429/14731</a>* @see isPrimitive*/function getTypeInfo(value){if (value === null)return new VariableType("null");const typeOfValue = typeof (value);const isPrimitive = typeOfValue !== "function" && typeOfValue !== "object";if (isPrimitive)return new VariableType(typeOfValue);const objectToString = Object.prototype.toString.call(value).slice(8, -1);// eslint-disable-next-line @typescript-eslint/ban-typesconst valueToString = value.toString();if (objectToString === "Function"){// A function or a constructorconst indexOfArrow = valueToString.indexOf("=>");const indexOfBody = valueToString.indexOf("{");if (indexOfArrow !== -1 && (indexOfBody === -1 || indexOfArrow < indexOfBody)){// Arrow functionreturn new VariableType("function");}// Anonymous and named functionsconst functionName = functionNamePattern.exec(valueToString);if (functionName !== null && typeof (functionName[1]) !== "undefined"){// Found a named function or class constructorreturn new VariableType("function", functionName[1].trim());}const className = classNamePattern.exec(valueToString);if (className !== null && typeof (className[1]) !== "undefined"){// When running under ES6+return new VariableType("class", className[1].trim());}// Anonymous functionreturn new VariableType("function");}if (objectToString === "Array")return new VariableType("array");
const classInfo = getTypeInfo(value.constructor);return new VariableType("object", classInfo.name);}
function UserFunction(){}
function UserClass(){}
let anonymousFunction = function(){};
let arrowFunction = i => i + 1;
console.log("getTypeInfo(undefined): " + getTypeInfo(undefined));console.log("getTypeInfo(null): " + getTypeInfo(null));console.log("getTypeInfo(true): " + getTypeInfo(true));console.log("getTypeInfo(5): " + getTypeInfo(5));console.log("getTypeInfo(\"text\"): " + getTypeInfo("text"));console.log("getTypeInfo(userFunction): " + getTypeInfo(UserFunction));console.log("getTypeInfo(anonymousFunction): " + getTypeInfo(anonymousFunction));console.log("getTypeInfo(arrowFunction): " + getTypeInfo(arrowFunction));console.log("getTypeInfo(userObject): " + getTypeInfo(new UserClass()));console.log("getTypeInfo(nativeObject): " + getTypeInfo(navigator.mediaDevices.getUserMedia));