JS基础——数据类型判断typeof、instanceof、Object.prototype.toString

  1. typeof用在基本数据类型和函数时,返回其对应类型的描述,对于引用类型都返回为object.
  2. instanceof无法判断基本数据类型,对于引用类型数据,返回其其对应类型。
  3. Object.prototype.toString无论基本数据类型还是引用类型返回其对应类型。

对应测试结果如下:

typeof testinstanceofObject.prototype.toString.call(test)
var test = 'xuriliang';stringtest instanceof String //false [object String]
var test = 27;numbertest instanceof Number //false [object Number]
var test = true;booleantest instanceof Boolean //false [object Boolean]
var test = [1,2,3];objecttest instanceof Array //true [object Array]
test instanceof Object //true
var test = null;objecttest instanceof Object //false [object Null]
var test = undefined;undefinedtest instanceof Object //false [object Undefined]
var test = new String('xuriliang')objecttest instanceof String //true [object String]
test instanceof Object //true
var test = new Number(27)objecttest instanceof Number //true [object Number]
test instanceof Object //true
var test = new Boolean(true)objecttest instanceof Boolean //true [object Boolean]
test instanceof Object //true
var test = new Array(1,2,3)objecttest instanceof Array //true [object Array]
test instanceof Object //true
var test = function(){}functiontest instanceof Function //true [object Function]
test instanceof Object //true
var test = /d/objecttest instanceof RegExp //true [object RegExp]
test instanceof Object //true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值