javascript判断数据类型
时间:2009年08月21日作者:愚人码头查看次数:4,433 views评论次数:1
今天在封装MTJS的时候出现了一个问题,用于检查数据类型的typeof在检查数组和对象的时候出来的都是“object”;例如
alert(typeof []);
alert(typeof {});
赶紧问朋友,朋友推荐我使用 pjhome的方法,原来这个方法EXT框架上也有的:
function getType(o) {
var _t;
return ((_t = typeof(o)) == "object" ? o==null && "null" || Object.prototype.toString.call(o).slice(8,-1):_t).toLowerCase();}
alert(getType("abc")); //string
alert(getType(true)); //boolean
alert(getType(123)); //number
alert(getType([])); //array
alert(getType({})); //object
alert(getType(function(){})); //function
alert(getType(new Date)); //date
alert(getType(new RegExp)); //regexp
alert(getType(Math)); //math
alert(getType(null)); //null


最新评论
非常好
Post:2012-02-08 21:59:12@linda, 没见过世面吧你,老板才8 »
Post:2012-02-07 18:11:22码头哥,换个链接,http://www.ijo »
Post:2012-02-07 17:18:45currying: 科里化
Post:2012-02-06 15:51:18貌似在线api已经下不了了
Post:2012-02-06 11:25:19图片被防盗链了,哈哈~
Post:2012-02-06 10:29:45对你造成的不便非常抱歉,简易你卸 »
Post:2012-02-05 14:45:16您好,请问air版本的自动更新怎么去 »
Post:2012-02-05 12:06:24