A simple method (recommended) for js to judge various data types. js Data Types
Anyone who knows about js knows that there is a typeof used to judge various data types. There are two ways to write: typeof xxx, typeof (xxx)
Example:
Typeof 2 output number
Typeof null Output object
Typeof {} output object
Typeof [] Output object
Typeof (function () {}) Output function
Typeof undefined output undefined
Typeof '200' output string
Typeof true outputs boolean
Five data types in js are included in this section: number string boolean undefinedobject and function type function.
Here you will certainly ask: how can I differentiate objects, arrays, and null?
Next we will use another tool: Object. prototype. toString. call
This is a native prototype Extension function of the object, which is used to distinguish data types more accurately.
Let's try this fun:
Var gettype = Object. prototype. toString
Gettype. call ('aaa') Outputs [object String]
Gettype. call (2222) Output [object Number]
Gettype. call (true) Output [object Boolean]
Gettype. call (undefined) Output [object Undefined]
Gettype. call (null) Output [object Null]
Gettype. call ({}) Output [object Object]
Gettype. call ([]) Output [object Array]
Gettype. call (function () {}) output [object Function]
We have solved the problem.
In fact, there are many types of judgment in js
[Object HTMLDivElement] div object,
[Object HTMLBodyElement] body object,
[Object Document] (IE) or
[Object HTMLDocument] (firefox, google )......
Various dom node judgments are used when we write plug-ins.
The encapsulation method is as follows:
var gettype=Object.prototype.toStringvar utility={isObj:function(o){ return gettype.call(o)=="[object Object]"; }, isArray:function(o){ return gettype.call(o)=="[object Array]"; }, isNULL:function(o){ return gettype.call(o)=="[object Null]"; }, isDocument:function(){ return gettype.call(o)=="[object Document]"|| [object HTMLDocument]; } ........}
The simple method (recommended) for judging various data types in js is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for the customer's house.