JavaScript gets variable type of function _javascript technique

Source: Internet
Author: User
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];
}

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.