判断Javascript变量类型的函数
作者:puterjam 日期:2009-07-29
toString 本来是用来做字符串转换的,不过现在流行用来做变量类型的检查了。舜子这里也写了一个函数,方便检查变量的类型,可以用来代替 typeof 
function getType(o) {
var _t; return ((_t = typeof(o)) == "object" ? o==null && "null" || Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();
}
var _t; return ((_t = typeof(o)) == "object" ? o==null && "null" || Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();
}
执行结果:
getType("abc"); //string
getType(true); //boolean
getType(123); //number
getType([]); //array
getType({}); //object
getType(function(){}); //function
getType(new Date); //date
getType(new RegExp); //regexp
getType(Math); //math
getType(null); //null
评论: 7 | 引用: 0 | 查看次数: -
回复
]仁兄,做个友链,已做好贵站链接。。www.men-ideal.com
不能发代码的吗??
什么评论中包含屏蔽字符
什么评论中包含屏蔽字符
null是object, Object是function
能改进下么?
puterjam 于 回复
能改进下么?
嘿嘿谢谢提醒,加上了对null的特殊处理
null呢
不错,犀牛书里面有讲 

- -#
看不懂撒
好久没来咯
o ho?
发现有变化..
标题?带阴影效果了
看不懂撒
好久没来咯
o ho?
发现有变化..
标题?带阴影效果了

为什么不用typeof呢?
puterjam 于 回复
typeof 无法准确判断 object 的类型呀
发表评论
上一篇
下一篇

文章来自:
Tags:
相关日志: