Home > Web Front-end > JS Tutorial > JavaScript data type detection code sharing_javascript skills

JavaScript data type detection code sharing_javascript skills

WBOY
Release: 2016-05-16 16:18:01
Original
1092 people have browsed it

Copy code The code is as follows:

/**
* param: o represents the detected value
* return: Returns the string "undefined", "number", "boolean", "string", "function", "regexp", "array", "date", "error", "object" or "null"
​*/
function typeOf(o){
var _toString = Object.prototype.toString; //Get the toString() method reference of the object
//List basic data types and built-in object types. You can further supplement 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"); //By converting the value to a string, and then matching whether the returned string contains a specific Characters are detected
}
//Application example:
var a = Math.abs;
alert(typeOf(a)); //return string "function"

The code is very simple, and the instructions are all in the comments. I won’t go into too much nonsense here. Friends who have the same needs can refer to it for their own reference

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template