JS基础 08-数据类型检测

129 阅读1分钟

数据类型检测

  • typeof [value] 检测数据类型的运算符
  • [value] instanceof [class] 检测某个实例是否属于这个类
  • [value].constructor === [class] 检测实例和类的关系,从而检测数据类型
  • Object.prototype.toString.call([value]) 检测数据类型
typeof 1  // 'number'
typeof NaN  // 'number'
typeof Infinity  // 'number'
typeof Symbol('AA')  // 'symbol'
typeof BigInt(13)  // 'bigint'

typeof null  // 'object',浏览器会把前三位的 二进制,识别为 object,null的前三位是 000(空对象指针)
typeof undefined  // 'undefined'
typeof function(){}  // 'function'
typeof {}  // 'object'
typeof []  // 'object'

typeof typeof typeof []  // 'string'

 if(x != null && typeof x == 'object') {
     // ...
 }

珠峰培训 - 40个小时彻底打实JavaScript基础 P21