浅谈JavaScript的类型判断
最近在看jQuery
源码的时候发现通过Object
下面的toString()
方法判断数据类型非常好使,大概是这样的
var arr = [];
console.log(Object.prototype.toString.call(arr)=='[object Array]');//true
var obj = {};
console.log(Object.prototype.toString.call(obj)=='[object Object]');//true
var date = new Date();
console.log(Object.prototype.toString.call(date)=='[object Date]');//true
var reg = new RegExp();
console.log(Object.prototype.toString.call(reg)=='[object RegExp]');//true
var oNull = null;
console.log(Object.prototype.toString.call(oNull)=='[object Null]');//[true