mannoroth 2011-12-15
http://blog.csdn.net/hongweigg/article/details/6824968
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>
用途:
很强大,也很实用,可用于判断服务端返回JSON串是否合法.如服务端返回数组类型JSON串,则eval()后的对象不是Array,则说明服务端处理出错了.