typeof检测数据类型

2022-04-16  本文已影响0人  maomizone

所有的数据类型值,在计算机底层都是安装64位的二进制进行存储的

Snipaste_2022-04-16_21-23-52.png
 console.log(a); // RHS查询,未找到报错 Uncaught ReferenceError: a is not defined
 console.log(typeof a); // undefined

typeof 使用场景

  1. 场景1
    判断是否是对象
const isObject = (obj) => {
  const type = typeof obj
 
  return obj !== null && (type === 'object' || type === 'function')
}
  1. 场景2
    支持更多的模块导入方案
 (function () {
        let utils = {}
        // 判断浏览器环境
        if (typeof window !== 'undefined') {
          window.utils = utils
        }

        // 判断nodejs环境
        if (typeof module === 'object' && typeof module.exports === 'object') {
          module.exports = utils
        }
})()
上一篇 下一篇

猜你喜欢

热点阅读