判断内置对象类型的几种方式优缺点详解

1.使用普通类型的typeof();

使用typeof只能判断出Object类型,toString会将内容有序打印出来,不能判断

2.使用toString();

toString会将内容有序打印出来,不能判断

3.使用(对象名).constructor.name

直接寻找对象的原型对象,然后找到对象的构造函数,再然后再取出其名字,准确

4.使用Object.prototype.toString.call(对象)

借调Object构造函数的原型链上面的方法,准确

判断内置对象Array的结果

  var arr=new Array(3);
    console.log(typeof(arr));
    console.log(arr.toString());
    console.log(arr.__proto__.constructor.name);
    console.log(Object.prototype.toString.call(arr)); 

效果:

判断内置对象Date的结果

<script>
    var arr=new Date();
    console.log(typeof(arr));
    console.log(arr.toString());
    console.log(arr.__proto__.constructor.name);
    console.log(Object.prototype.toString.call(arr));         
</script>

效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值