You need to write a bit about this function. Code . The following function can get a variable type. When you call a variable, a variable type described in the string format will be returned.
Copy code The Code is as follows: // obtain the type of x and return 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 return type name
If (T. tolocalelowercase ()! = "Object") return T;
// Call the tostring method of the object class to obtain the type information.
// The object. tostring method returns information similar to this [object class name]
T = object. Prototype. tostring. Apply (x). tolowercase ();
// Intercept the class name part returned by the tostring Method
T = T. substring (8, T. Length-1 );
If (T. tolocalelowercase ()! = "Object") return T;
// Check whether X is of the Object Type
If (X. constructor = Object) return T;
// Obtain the type name from the constructor
If (typeof X. constructor = "function ")
Return getfunctionname (X. constructor );
Return "unknow type ";
}
// Obtain 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.exe C (FN );
If (! Name ){
Return '(anonymous )';
}
Return name [1];
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service