Home > Web Front-end > JS Tutorial > js simple method to determine various data types

js simple method to determine various data types

高洛峰
Release: 2017-01-14 09:59:48
Original
884 people have browsed it

Anyone who knows js knows that there is a typeof used to determine various data types. There are two ways of writing: typeof xxx , typeof(xxx)

The following example:

typeof 2 Output number

typeof null Output object

typeof {} Output object

typeof [] Output object

typeof (function(){}) Output function

typeof undefined Output undefined

typeof '222' Output string

typeof true Output boolean

This contains five types of data in js Type number string boolean undefinedobject and function type function

You will definitely ask when you see this: How do I distinguish objects, arrays and null?

Next we will use another sharp tool : Object.prototype.toString.call

This is a native prototype extension function of the object, used to distinguish data types more accurately.

Let’s try this thing:

var gettype=Object.prototype.toString

gettype.call('aaaa') output [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]

Seeing this, we have solved the problem just now.

In fact, there are many type judgments in js

[object HTMLDivElement] div object,
[object HTMLBodyElement] body object,
[object Document](IE) or
[object HTMLDocument] (firefox, google) ...

Judgement of various DOM nodes, these things will be used when we write plug-ins.

The methods that can be encapsulated are as follows:

var  gettype=Object.prototype.toString
 
var  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];
 
     }
 
     ........
 
}
Copy after login

The above js simple method (recommended) for judging various data types is all the content shared by the editor. I hope it can give you a For reference, I also hope that everyone will support the PHP Chinese website.

For more js simple methods to determine various data types, please pay attention to the PHP Chinese website for related articles!

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