JavaScript data type detection code sharing _javascript Tips

Source: Internet
Author: User

Copy Code code as follows:

/**
* Param:o indicates the value of the detection
* Return: Returns the string "undefined", "Number", "Boolean", "string", "function", "regexp", "array", "Date", "Error", "Object" or "null" "
*/
function TypeOf (o) {
var _tostring = Object.prototype.toString; Gets the object's ToString () method reference
Enumerating the basic data types and built-in object types, you can further supplement the array's detection data type range
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"); Detect by converting a value to a string and then matching the return string to include a specific character
}
application Example:
var a = Math.Abs;
Alert (TypeOf (a)); Returns the string "function"

The code is very simple, the description is in the comments, here is not much nonsense, have the same needs of the small partners themselves to refer to the next bar

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.