The data type of jquery used previously was judged, such as: IsArray (), and so on, today we see a simple native method of judging the data type and share it with everyone
Object.prototype.toString method returns the type string of an object, so it can be used to determine the type of a value
var val =
object. Prototype. Tostring. Call ({}) //"[Object Object]" object. Prototype. Tostring. Call ([]) //"[Object Array]"
Object.prototype.toString.call(2) // "[object Number]"Object.prototype.toString.call(‘‘) // "[object String]"
var type = Val.match (/\[object (. *?) \]/) [1].tolowercase ();//Type of data
On the basis of this, you can also add a method that specifically judges some type of data.
var Typearr = [' Null ', ' Undefined ', ' Object ', ' Array ', ' String ', ' number ', ' Boolean ', ' Function ', ' RegExp ' ]; var type = {}; Typearr.foreach (function (t) { type[' is ' + t] = function (params) { var val = Object.prototype.toString.call (PA Rams); var type = Val.match (/\[object (. *?) \]/) [1].tolowercase (); return type = = = T.tolowercase (); } })
From the JavaScript standard reference tutorial (Alpha), by Ruan Feng
JS native data type judgment