首页 > 其他 > 详细

数据类型检测

时间:2020-11-10 10:07:03      阅读:22      评论:0      收藏:0      [点我收藏+]

typeof

  • 基本类型返回的都是小写的字符串
  • 引用类型无法区分是普通对象还是数组对象,返回都是‘object‘,函数是‘function‘
typeof [];  //  ‘object‘
typeof {};  //  ‘object‘
typeof true // ‘boolean‘
typeof 1; //  ‘number‘
typeof NaN;  //  ‘number‘
typeof ‘‘  //  ‘string‘
typeof null //  ‘object‘
typeof undefined //  ‘undefined‘
typeof function () {};  // ‘function‘

instanceof


Object.prototype.toString.call()

  • 这个是检测数据类型最好,最全面的的方法了
Object.prototype.toString()      //  ‘[object Object]‘
Object.prototype.toString.call()      //  "[object Undefined]"
Object.prototype.toString.call([])      //  ‘[object Array]‘  注意第二个是大写开头
Object.prototype.toString.call({})      //  ‘[object Object]‘
Object.prototype.toString.call(true)      //  ‘[object Boolean]‘
Object.prototype.toString.call(1)      //  ‘[object Number]‘
Object.prototype.toString.call(NaN)   //  ‘[object Number]‘
Object.prototype.toString.call(‘‘)       //  ‘[object String]‘
Object.prototype.toString.call(null)       //  ‘[object Null]‘
Object.prototype.toString.call(undefined)      //  ‘[object Undefined]‘

数据类型检测

原文:https://www.cnblogs.com/flyerya/p/13951768.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!