javaScript 类型判断

直接上例子:

1 判断是否为数组类型
2 判断是否为字符串类型
3 判断是否为数值类型
4 判断是否为日期类型
5 判断是否为函数
6 判断是否为对象
 
1 判断是否为数组类型
linenum
<script type="text/javascript">
//<![CDATA[
var a=[0];
document.write(isArray(a),'<br/>');
function isArray(obj){
return (typeof obj=='object')&&obj.constructor==Array;
}
//]]>
</script>
2 判断是否为字符串类型
linenum
<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>
3 判断是否为数值类型
linenum
<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>
4 判断是否为日期类型
linenum
<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>
5 判断是否为函数
linenum
<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>
6 判断是否为对象
<script type="text/javascript">
linenum
//<![CDATA[
document.write(isObject(new Object()),'<br/>');
document.write(isObject(10),'<br/>');
function isObject(obj){
return (typeof obj=='object')&&obj.constructor==Object;
}
//]]>
</script>


你可能感兴趣的:(JavaScript)