美文网首页Web前端之路前端开发
JavaScript 超简单判断类型

JavaScript 超简单判断类型

作者: 梁同桌 | 来源:发表于2019-05-30 18:09 被阅读5次

前言

js 对于基础类型判断非常弱,网上方法很乱且不好记,又不愿意用库。

__ js 类型__

NaN 
null undefined 
Object String Number Boolean 
Array Function Symbol

__ 一行判断方法:__

isNaN(NaN) //true ,特殊

Object.prototype.toString.call(null) ;           // [object Null]
Object.prototype.toString.call(undefined) ;      // [object Undefined]

Object.prototype.toString.call({}) ;             // [object Object]
Object.prototype.toString.call('') ;             // [object String]
Object.prototype.toString.call(1) ;              // [object Number]
Object.prototype.toString.call(true) ;           // [object Boolean]
   
Object.prototype.toString.call([]) ;             // [object Array]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(Symbol());        // [object Symbol]

相关文章

网友评论

    本文标题:JavaScript 超简单判断类型

    本文链接:https://www.haomeiwen.com/subject/thxbtctx.html