SoFunction
Updated on 2025-04-10

Javascript to get variable type function


//Get the type of x, return the type name
function getType(x) {
//If x is null, return null
if (x == null) return "null";
var t = typeof x;
//If x is a simple type, return the type name
if (() != "object") return t;
//Calling the toString method of the object class to get type information
//The method returns information like this [object class name]
t = (x).toLowerCase();
//Intercept the class name part of the return value of the toString method
t = (8, - 1);
if (() != "object") return t;
//Check that x is indeed object type
if ( == Object) return t;
//Get type name from the constructor
if (typeof == "function")
return getFunctionName();
return "unknow type";
}
//Get the function name
function getFunctionName(fn) {
if (typeof fn != "function") throw "the argument must be a function.";
var reg = /\W*function\s+([\w\$]+)\s*\(/;
var name = (fn);
if (!name) {
return '(Anonymous)';
}
return name[1];
}