JavaScript data type detection code sharing, javascript Data Type

Source: Internet
Author: User

JavaScript data type detection code sharing, javascript Data Type

Copy codeThe Code is as follows:
/**
* Param: o indicates the detected value.
* Return: returns the "undefined", "number", "boolean", "string", "function", "regexp", "array", "date", "error", and "object" strings. "or" null"
*/
Function typeOf (o ){
Var _ toString = Object. prototype. toString; // obtain the toString () method reference of the Object
// Lists the basic data types and built-in object types. You can also add the detection data type range of the array.
Var _ type = {
"Undefined": "undefined ",
"Number": "number ",
"Boolean": "boolean ",
"String": "string ",
"[Object Function]": "function ",
"[Object RegExp]": "regexp ",
"[Object Array]": "array ",
"[Object Date]": "date ",
"[Object Error]": "error"
}
Return _ type [typeof o] | _ type [_ toString. call (o)] | (o? "Object": "null"); // checks whether the returned string contains specific characters by converting the value to a string.
}
// Application example:
Var a = Math. abs;
Alert (typeOf (a); // returns the string "function"

The code is very simple. The instructions are all in comments. There is not much nonsense here. If you have the same requirements, refer to them.

Related Article

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.