数据类型判断方式

1. 有哪些数据类型

JS 中共有7种数据类型,包括基本类型(原始类型)和引用类型。

  • 其中,基本数据类型有7种,分别是:string、number、boolean、undefined、null、symbol、bigInt
    symbol 是 ES6 新增的数据类型,表示独一无二的值,通过 symbol() 函数生成(不能通过 new 调用)
  • 引用类型有object、function、array

2. 怎么区分数据类型

(1)typeof
通过 typeof 运算符(返回字符串),可以准确的判断基础数据类型和function,但不能区分object类型的具体类型,比如 Array 、Date 以及自定义类

typeof (() => {}) // function
typeof [] // object

// 实现typeof
function _typeof(params) {
  const res = Object.prototype.toString.call(params).slice(8, -1)
  let map = {
    Number: 'number',
    Array: 'object',
    String: 'string',
    Object: 'object',
    Null: 'null',
    undefined: 'undefined',
    Symbol: 'symbol',
    Boolean: 'boolean',
    Function: 'function'
  }
  return map[res]
}

(2)instanceof
通过 instanceof 关键字来实现(返回布尔值),本意是用来判断 A 是否为 B 的实例对象。能够正确判断[] 是Array的实例对象,但不能辨别 [] 不是Object的实例对象。不能判断基本数据类型

(() => {}) instanceof Object // true
[] instanceof Object // true

// 实现instanceof
function new_instance_of(leftVaule, rightVaule) {
  let rightProto = rightVaule.prototype;
  leftVaule = leftVaule.__proto__;
  while (true) {
    if (leftVaule === null) {
      return false;
    }
    if (leftVaule === rightProto) {
      return true;
    }
    leftVaule = leftVaule.__proto__
}

(3)constructor
constructor 原本就是用来进行对象类型判断的,但是还有一个作用就是:每一个对象实例都可以通过 constrcutor 对象访问它的构造函数
判断引用类型,不能判断基本数据类型

let arr = function fn() {}
console.log(arr.constructor);  // ƒ Function() { [native code] }

console.log(Function.prototype.constructor === Function); // true

以上这三种方法都不是区分数据类型的最佳实现

(4)Object.prototype.toString()

Object.prototype.toString.call({})              // '[object Object]'
Object.prototype.toString.call([])              // '[object Array]'
Object.prototype.toString.call(() => {})        // '[object Function]'
Object.prototype.toString.call('seymoe')        // '[object String]'
Object.prototype.toString.call(1)               // '[object Number]'
Object.prototype.toString.call(true)            // '[object Boolean]'
Object.prototype.toString.call(Symbol())        // '[object Symbol]'
Object.prototype.toString.call(null)            // '[object Null]'
Object.prototype.toString.call(undefined)       // '[object Undefined]'

Object.prototype.toString.call(new Date())      // '[object Date]'
Object.prototype.toString.call(Math)            // '[object Math]'
Object.prototype.toString.call(new Set())       // '[object Set]'
Object.prototype.toString.call(new WeakSet())   // '[object WeakSet]'
Object.prototype.toString.call(new Map())       // '[object Map]'
Object.prototype.toString.call(new WeakMap())   // '[object WeakMap]'

// 更显眼的方式
function checkType(obj) {
  return Object.prototype.toString.call(obj).slice(8,-1)
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Vue中,判断数据类型可以使用typeof操作符或instanceof操作符。使用typeof操作符可以判断基本数据类型,例如number、boolean、string、function、object和undefined。但是它不能准确判断null和array的类型。例如,typeof null的结果是'object',typeof array的结果也是'object'。对于判断对象类型,可以使用instanceof操作符。例如,判断一个变量是否是Vue组件,可以使用变量名 instanceof Vue。这种方式可以准确判断对象的类型。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Vue前端面试题总结(二) 数据类型判断详解](https://blog.csdn.net/Rick_and_mode/article/details/108600279)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [前端 vue js判断数据类型常用的方法 [typeof,instanceof,prototype,constructor,jquery.type(), ===]](https://blog.csdn.net/weixin_44972441/article/details/122347004)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值