js判断数据类型如判断是否为数组是否为字符串等等
1 判断是否为数组类型
2 判断是否为字符串类型
3 判断是否为数值类型 !isNaN(变量) 如果为true就是数字类型
4 判断是否为日期类型
5 判断是否为函数
6 判断是否为对象
一.判断是否为数组类型
<script type="text/javascript">
//<![CDATA[
var a=[0];
document.write(isArray(a),'<br/>');
function isArray(obj){
return (typeof obj=='object')&&obj.constructor==Array;
}
//]]>
</script>
var a=[0];
document.write(isArray(a),'<br/>');
function isArray(obj){
return (typeof obj=='object')&&obj.constructor==Array;
}
//]]>
</script>
二.判断是否为字符串类型
<script type="text/javascript">
//<![CDATA[
document.write(isString('test'),'<br/>');
document.write(isString(10),'<br/>');
function isString(str){
return (typeof str=='string')&&str.constructor==String;
}
//]]>
</script>
//<![CDATA[
document.write(isString('test'),'<br/>');
document.write(isString(10),'<br/>');
function isString(str){
return (typeof str=='string')&&str.constructor==String;
}
//]]>
</script>
三.判断是否为数值类型
<script type="text/javascript">
//<![CDATA[
document.write(isNumber('test'),'<br/>');
document.write(isNumber(10),'<br/>');
function isNumber(obj){
return (typeof obj=='number')&&obj.constructor==Number;
}
//]]>
</script>
//<![CDATA[
document.write(isNumber('test'),'<br/>');
document.write(isNumber(10),'<br/>');
function isNumber(obj){
return (typeof obj=='number')&&obj.constructor==Number;
}
//]]>
</script>
四.判断是否为日期类型
<script type="text/javascript">
//<![CDATA[
document.write(isDate(new Date()),'<br/>');
document.write(isDate(10),'<br/>');
function isDate(obj){
return (typeof obj=='object')&&obj.constructor==Date;
}
//]]>
</script>
document.write(isDate(new Date()),'<br/>');
document.write(isDate(10),'<br/>');
function isDate(obj){
return (typeof obj=='object')&&obj.constructor==Date;
}
//]]>
</script>
五.判断是否为函数
<script type="text/javascript">
//<![CDATA[
document.write(isFunction(function test(){}),'<br/>');
document.write(isFunction(10),'<br/>');
function isFunction(obj){
return (typeof obj=='function')&&obj.constructor==Function;
}
//]]>
</script>
document.write(isFunction(function test(){}),'<br/>');
document.write(isFunction(10),'<br/>');
function isFunction(obj){
return (typeof obj=='function')&&obj.constructor==Function;
}
//]]>
</script>
六.判断是否为对象
<script type="text/javascript">
//<![CDATA[
document.write(isObject(new Object()),'<br/>');
document.write(isObject(10),'<br/>');
function isObject(obj){
return (typeof obj=='object')&&obj.constructor==Object;
}
//]]>
</script>
document.write(isObject(new Object()),'<br/>');
document.write(isObject(10),'<br/>');
function isObject(obj){
return (typeof obj=='object')&&obj.constructor==Object;
}
//]]>
</script>
摘抄路径:http://www.jb51.net/article/45731.htm