Home >Web Front-end >JS Tutorial >Summary of examples of methods for determining data types in JavaScript

Summary of examples of methods for determining data types in JavaScript

伊谢尔伦
伊谢尔伦Original
2017-07-18 09:23:031164browse

js contains five data types number string boolean undefinedobject and function type function

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

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

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

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 HTMLpElement] p 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];
     }
     ........
}

1 Determine whether it is an array type

<script type="text/javascript"> 
//<![CDATA[ 
var a=[0]; 
document.write(isArray(a),&#39;<br/>&#39;); 
function isArray(obj){ 
return (typeof obj==&#39;object&#39;)&&obj.constructor==Array; 
} 
//]]> 
</script>

2 Determine whether It is a string type

<script type="text/javascript"> 
//<![CDATA[ 
document.write(isString(&#39;test&#39;),&#39;<br/>&#39;); 
document.write(isString(10),&#39;<br/>&#39;); 
function isString(str){ 
return (typeof str==&#39;string&#39;)&&str.constructor==String; 
} 
//]]> 
</script>

3 Determine whether it is a numeric type

<script type="text/javascript"> 
//<![CDATA[ 
document.write(isNumber(&#39;test&#39;),&#39;<br/>&#39;); 
document.write(isNumber(10),&#39;<br/>&#39;); 
function isNumber(obj){ 
return (typeof obj==&#39;number&#39;)&&obj.constructor==Number; 
} 
//]]> 
</script>

The above is the detailed content of Summary of examples of methods for determining data types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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