日志标签:判断数据类型

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

继续阅读:javascript判断数据类型»

标签:分类:JS
Page 1 of 11