js中对数据类型的判断

其中检测的方法有:

typeof、instanceof、constructor、Object.prototype.toString().call()、query.type()。

1. typeof检测分析
  1. typeof 检测基本类型除了null和undefined以外,都会检测出对应的类型,
    然而null检测出来的是object , undefined检测出来的只是undefined
    此外,typeof如果检测引用类型中的function那么返回的类型还是function。

  2. 用 typeof 检测构造函数创建的Number,String,Boolean都返回object。

  3. 引用数据类型中的:Array,Object,Date,RegExp。不可以用typeof检测。都会返回小写的object。

console.log(
    typeof 100, //"number"
    typeof 'abc', //"string"
    typeof false, //"boolean"
    typeof undefined, //"undefined"
    typeof null, //"object"
    typeof [1,2,3], //"object"
    typeof {a:1,b:2,c:3}, //"object"
    typeof function(){console.log('aaa');}, //"function"
    typeof new Date(), //"object"
    typeof /^[a-zA-Z]{5,20}$/, //"object"
    typeof new Error() //"object"
    typeof new Number(100), //'object'
    typeof new String('abc'),// 'string'
    typeof new Boolean(true),//'boolean'
);
2. instanceof检测分析:

instanceof 检测基本数据类型,返回的是boolean类型中的false;如果检测基本数据类型中的null和undefined,返回的也是false,因为他们类型就是本身并不是Object创建出了它们。而检测引用数据类型返回的是boolean类型中的true。 然而字面量值不可以用instanceof检测,而构造函数创建的值可以。

console.log(
    100 instanceof Number, //false
    'dsfsf' instanceof String, //false
    false instanceof Boolean, //false
    undefined instanceof Object, //false
    null instanceof Object, //false
    [1,2,3] instanceof Array, //true
    {a:1,b:2,c:3} instanceof Object, //true
    function(){console.log('aaa');} instanceof Function, //true
    new Date() instanceof Date, //true
    /^[a-zA-Z]{5,20}$/ instanceof RegExp, //true
    new Error() instanceof Error //true
)
3.Object.prototype.toString().call()

我们可以通过 toString() 来获取每个对象的类型,
为了每个对象都能通过Object.prototype.toString()来检测,
需要以 Function.prototype.call()Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数。

var aa = Object.prototype.toString;
console.log(aa.call(123));//[object Number]
console.log(aa.call('123'));//[object String]
console.log(aa.call([1,2,3,4,5]));//[object Array]
console.log(aa.apply([1,2,3,4,5]));//[object Array]
console.log(aa.call({age:16}));//[object Object]
console.log(aa.call(undefined));//[object Undefined]
console.log(aa.call(null));//[object Null]
console.log(aa.apply(null));//[object Null]
console.log(aa.call(new Date()));//[object Date]
console.log(aa.call(/^[a-zA-Z]{5,20}$/));//[object RegExp]
console.log(aa.call(new Error()));//[object Error]
4. constructor

constructor是prototype对象上的属性,指向构造函数。根据实例对象寻找属性的顺序,
若实例对象上没有实例属性或方法时,就去原型链上寻找。实例对象也是能使用constructor属性的。

//实例代码展示:
console.log(new Number(666).constructor);
ƒ Number() { [native code] }
//构造函数展示:
var num  = 123;
var str  = 'abcdef';
var bool = true;
var arr  = [1, 2, 3, 4];
var json = {name:'wenzi', age:25};
var func = function(){ console.log('this is function'); }
var und  = undefined;
var nul  = null;
var date = new Date();
var reg  = /^[a-zA-Z]{5,20}$/;
var error= new Error();

function Person(){
  
}
var tom = new Person();

// undefined和null没有constructor属性
console.log(
    tom.constructor==Person,
    num.constructor==Number,
    str.constructor==String,
    bool.constructor==Boolean,
    arr.constructor==Array,
    json.constructor==Object,
    func.constructor==Function,
    date.constructor==Date,
    reg.constructor==RegExp,
    error.constructor==Error
);
//所有结果均为true
5. 万能的方法:jquery.type()
在判断前,需要我们先引入jquery插件,然后再操作进行数据判断其类型;
console.log($.type( undefined ) === "undefined");//true
console.log($.type(window.notDefined));//undefined
console.log($.type());//undefined
console.log($.type(null));//null
console.log($.type(3));//number
console.log($.type('6'));//string
console.log($.type([]));//array
console.log($.type(new Date()));//date
console.log($.type(new Error()));//error
console.log($.type(/test/));//regexp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值