您当前的位置: 首页 >  小程序

培根芝士

暂无认证

  • 0浏览

    0关注

    446博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

小程序类型判断

培根芝士 发布时间:2021-07-13 18:43:41 ,浏览量:0

方法1:Object.prototype.toString.call(x)

console.log(Object.prototype.toString.call(10) == '[object Number]')
console.log(Object.prototype.toString.call(str) == '[object String]')
console.log(Object.prototype.toString.call([1,2,3,4]) == '[object Array]')
console.log(Object.prototype.toString.call({name: 'a'}) == '[object Object]')
console.log(Object.prototype.toString.call(new Date()) == '[object Date]')
console.log(Object.prototype.toString.call(undefined) == '[object Undefined]')
console.log(Object.prototype.toString.call(null) == '[object Null]')

方法2:typeof(x)

console.log(typeof(1) == 'number')
console.log(typeof('1') == 'string')
console.log(typeof([1,2,3]) == 'object')
console.log(typeof({name:'1'}) == 'object')
console.log(typeof(new Date()) == 'object')
console.log(typeof(undefined) == 'undefined')
console.log(typeof(null) == 'object')

关注
打赏
1660824269
查看更多评论
立即登录/注册

微信扫码登录

0.0363s