如何选择最优雅的JavaScript类型判断方法?

2026-04-02 22:231阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计575个文字,预计阅读时间需要3分钟。

如何选择最优雅的JavaScript类型判断方法?

JavaScript有8种数据类型:值类型和引用类型。值类型包括Number、Null、Undefined、String、Symbol、Boolean、BigInt;引用类型包括Object、Array、Function。判断数据类型有以下几种方法:

第一种方法:使用typeof操作符

- typeof null 返回 object- typeof undefined 返回 undefined

直接输出结果:typeof null --- object; typeof undefined --- undefined

javascript有8种数据类型

值类型

  • Number
  • Null
  • Undefined
  • String
  • Symbol
  • Boolean
  • BigInt

引用类型

  • Object
  • Array
  • Function

判断数据类型有以下4种判断方法

第一种方式:typeof

typeof null ---> "object"  typeof undefined ---> "undefined"  typeof true | false ---> 'boolean'  typeof 42 ---> 'number'  typeof "42" ---> 'string'  typeof { name : '1'} | [] ---> 'object'  typeof Symbol ---> 'symbol'  typeof ()=>{} ---> 'function'  typeof void 0 ---> 'undefined'

第二种方式 instanceof 但是这种方式只适合判断object类型

instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上

详细介绍请看这里:javascript中的instanceof运算符

比如 : var arr = [] ; arr instanceof Array ---> true null instanceof Object ---> false [function] instanceof Object | Function --> true

第三种方式 Object.prototype.toString.call() 这种方式可以将全部的数据类型检测出来 也是 推荐的方式

因为toString是Object的原型方法, 而 Array Function 等都是Object的实例。都重写了toString 方法。返回的是类型的字符串

Object.prototype.toString.call(null) ---> [object Null]  Object.prototupe.toString.call(undefined) ---> [object Undefined]  Object.prototype.toString.call(123) ---> [object Number]  Object.prototype.toString.call(true) ---> [object Boolean]  Object.prototype.toString.call('123') ---> [object String]  Object.prototype.toString.call({}) ---> [object Object]  Object.prototype.toString.call([]) ---> [object Array]  Object.prototype.toString.call(Math) ---> [object Math]  Object.prototype.toString.call(function(){}) ---> [object Function]  Objdec.prototype.toString.call(new Date) ---> [object Date]  Object.prototype.toString.call(Symbol()) ---> [object Symbol]

第四种方式: constructor 判断对象的构造函。

1. null 是js 原型链的起点,没有构造函数   2. undefined 没有构造函数   3. [].constructor === Array ---> true   4. [string].constructor === String   5. [object].constructor === object   6. [number].constructor === Number   7. [symbol].constructor === Symbol   8. [function].constructor === Function   9. [new Date].constructor === Date   10. [RegExp].constructor === RegExp

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

如何选择最优雅的JavaScript类型判断方法?

本文共计575个文字,预计阅读时间需要3分钟。

如何选择最优雅的JavaScript类型判断方法?

JavaScript有8种数据类型:值类型和引用类型。值类型包括Number、Null、Undefined、String、Symbol、Boolean、BigInt;引用类型包括Object、Array、Function。判断数据类型有以下几种方法:

第一种方法:使用typeof操作符

- typeof null 返回 object- typeof undefined 返回 undefined

直接输出结果:typeof null --- object; typeof undefined --- undefined

javascript有8种数据类型

值类型

  • Number
  • Null
  • Undefined
  • String
  • Symbol
  • Boolean
  • BigInt

引用类型

  • Object
  • Array
  • Function

判断数据类型有以下4种判断方法

第一种方式:typeof

typeof null ---> "object"  typeof undefined ---> "undefined"  typeof true | false ---> 'boolean'  typeof 42 ---> 'number'  typeof "42" ---> 'string'  typeof { name : '1'} | [] ---> 'object'  typeof Symbol ---> 'symbol'  typeof ()=>{} ---> 'function'  typeof void 0 ---> 'undefined'

第二种方式 instanceof 但是这种方式只适合判断object类型

instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上

详细介绍请看这里:javascript中的instanceof运算符

比如 : var arr = [] ; arr instanceof Array ---> true null instanceof Object ---> false [function] instanceof Object | Function --> true

第三种方式 Object.prototype.toString.call() 这种方式可以将全部的数据类型检测出来 也是 推荐的方式

因为toString是Object的原型方法, 而 Array Function 等都是Object的实例。都重写了toString 方法。返回的是类型的字符串

Object.prototype.toString.call(null) ---> [object Null]  Object.prototupe.toString.call(undefined) ---> [object Undefined]  Object.prototype.toString.call(123) ---> [object Number]  Object.prototype.toString.call(true) ---> [object Boolean]  Object.prototype.toString.call('123') ---> [object String]  Object.prototype.toString.call({}) ---> [object Object]  Object.prototype.toString.call([]) ---> [object Array]  Object.prototype.toString.call(Math) ---> [object Math]  Object.prototype.toString.call(function(){}) ---> [object Function]  Objdec.prototype.toString.call(new Date) ---> [object Date]  Object.prototype.toString.call(Symbol()) ---> [object Symbol]

第四种方式: constructor 判断对象的构造函。

1. null 是js 原型链的起点,没有构造函数   2. undefined 没有构造函数   3. [].constructor === Array ---> true   4. [string].constructor === String   5. [object].constructor === object   6. [number].constructor === Number   7. [symbol].constructor === Symbol   8. [function].constructor === Function   9. [new Date].constructor === Date   10. [RegExp].constructor === RegExp

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

如何选择最优雅的JavaScript类型判断方法?