判断一个变量的类型Object.prototype.toString.call

var num = 1;
alert(Object.prototype.toString.call(num)); // [object Number]
var str = 'hudidit.com';
alert(Object.prototype.toString.call(str)); // [object String]
var boo = true;
alert(Object.prototype.toString.call(boo)); // [object Boolean]
var fun = function(){};
alert(Object.prototype.toString.call(fun)); // [object Function]
var obj = {};
alert(Object.prototype.toString.call(obj)); // [object Object]
var arr = [];
alert(Object.prototype.toString.call(arr)); // [object Array]

var n = null;

alert(Object.prototype.toString.call(n)); //[object Null]

var u = undefined;

alert(Object.prototype.toString.call(u));//[object Undefined]

posted @ 2016-11-28 14:53  向着太阳生  阅读(163)  评论(0编辑  收藏  举报