This function needs to write a little code to implement. The following function can get the type of a variable that is passed a variable in, and returns the type of the variable described in string form.
Copy Code code as follows:
Gets the type of x, returns the type name
function GetType (x) {
If x is null, NULL is returned
if (x = = null) return "NULL";
var t = typeof X;
If x is a simple type, the type name is returned
if (T.tolocalelowercase ()!= "Object") return t;
Call the object class's ToString method to get the type information
The Object.ToString method returns information like this [object class name]
t = Object.prototype.toString.apply (x). toLowerCase ();
Intercepts the class name part of the value returned by the ToString method
t = t.substring (8, t.length-1);
if (T.tolocalelowercase ()!= "Object") return t;
Check that x is really type Object
if (X.constructor = = Object) return t;
Get type name from constructor
if (typeof x.constructor = = "function")
Return Getfunctionname (X.constructor);
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 = Reg.exec (FN);
if (!name) {
Return ' (Anonymous) ';
}
return name[1];
}