Skip to content

数据类型

数据类型的介绍以及存储

JavaScript 共有 八种 数据类型,分别是 **Undefined, Null, Boolean,

Number, String, Object, Symbol, BigInt**

  • 两种类型的区别在于存储位置的不同:
    • 栈(stack):原始数据类型 Undefined、Null、Boolean、Number、String
    • 堆(heap):引用数据类型 对象(数组和函数)、Symbol、BigInt
    • Symbol, BigInt 这两个 "基础数据类型" 其实并不能说成是 基础数据类型 而是一个

      引用数据类型的 指针

      所以 这两个的引用指针是放在 stack 中 本身是放在 heap

  • 原始数据类型是直接存储在stack中的简单数据段, 占据空间小、大小固定,属于被频繁使用数据,所以放入栈中存储
  • 引用数据类型是存储在heap中的对象,占据空间大、大小不固定. 如果存储在stack中,将会影响程序运行的性能; 引用数据类型在stack中存储了指针,该指针指向堆中该实体的起始地址. 当解释器寻找引用值时,会首先检索其在stack中的地址,取得地址后从heap中获得实体.
  • 堆和栈的概念存在于数据结构操作系统内存中
    • 在数据结构中
      • 中数据的存取方式为 先进后出
      • 是一个优先队列,是按优先级来进行排序的,优先级可以按照大小来规定
    • 在操作系统中,内存被分为栈区堆区
      • 栈区内存由编译器自动分配释放,存放函数的参数值,局部变量的值等. 其操作方式类似于数据结构中的栈.
      • 堆区内存一般由开发者分配释放,若不释放,程序结束时 有可能垃圾回收机制 回收.

Undefined

未定义(Undefined): 表示一个未被定义的值.在变量被声明但未被赋值时,它的值为 undefined.

Null

空值(Null): 表示一个空的值.可以将变量赋值为 null 来表示它是空的.

Boolean

布尔(Boolean): 用于表示逻辑上的真或假.有两个值:true 和 false.

Number

数字(Number): 用于表示数字,可以是整数或者浮点数.例如:42,3.14.

String

字符串(String): 用于表示文本数据.例如:"hello","world".

Object

对象(Object): 是一种复合数据类型,可以用来表示任何对象.对象由一组属性(属性名和属性值)组成.例如:{ name: "张三", age: 18 }.

Symbol

Symbol: 表示一个独一无二的值,用于对象属性的键.

BigInt

BigInt: 用于表示任意精度的整数,可以超出 JavaScript 中 Number 类型所能表示的范围.

数据类型检测的方式

typeof

类型判断方案结果
'number'typeof 1number
'string'typeof '1'string
'boolean'typeof trueboolean
'function'typeof () => {}function
'undefined'typeof undefinedundefined
'symbol'typeof Symbol(1)symbol
'bigint'typeof BigInt(1)bigint
'null'typeof nullobject
'object'typeof {}object
'array'typeof []object
'date'typeof new Date()object
'regexp'typeof /1/object
'map'typeof new Map()object
'set'typeof new Set()object

instanceof

instanceof 可以正确判断对象的类型

其内部运行机制是判断在其原型链中能否找到该类型的原型.

类型判断方案结果
'undefined'instanceof报错
'null'instanceof报错
'number'1 instanceof Numberfalse
'string''1' instanceof Stringfalse
'boolean'true instanceof Booleanfalse
'symbol'Symbol(1) instanceof Symbolfalse
'bigint'BigInt(1) instanceof BigIntfalse
'object'{} instanceof Objecttrue
'array'[] instanceof Arraytrue
'function'() => {} instanceof Functiontrue
'date'new Date() instanceof Datetrue
'regexp'/1/ instanceof RegExptrue
'map'new Map() instanceof Maptrue
'set'new Set() instanceof Settrue

可以看到,instanceof 只能正确判断引用数据类型,而不能判断 基本数据类型.
instanceof 运算符可以用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性.

constructor

constructor 有两个作用,一是判断数据的类型,二是对象实例通过 constrcutor 对象访问它的构造函数.

类型判断方案结果
'undefined'constructor报错
'null'constructor报错
number(1).constructor === Numbertrue
string('1').constructor === Stringtrue
boolean(true).constructor === Booleantrue
object({}).constructor === Objecttrue
array([]).constructor === Arraytrue
function(() => { }).constructor === Functiontrue
date(new Date()).constructor === Datetrue
regexp(/1/).constructor === RegExptrue
symbol(Symbol(1)).constructor === Symboltrue
bigint(BigInt(1)).constructor === BigInttrue
map(new Map()).constructor === Maptrue
set(new Set()).constructor === Settrue

Object.prototype.toString.call

Object.prototype.toString.call() 使用 Object 对象的原型方法

toString 来判断数据类型

前提是不能重写 对象的 toString 方法 s

类型判断方案结果
undefinedObject.prototype.toString.call(undefined)[object Undefined]
nullObject.prototype.toString.call(null)[object Null]
numberObject.prototype.toString.call(1)[object Number]
stringObject.prototype.toString.call('1')[object String]
booleanObject.prototype.toString.call(true)[object Boolean]
objectObject.prototype.toString.call({})[object Object]
arrayObject.prototype.toString.call([])[object Array]
functionObject.prototype.toString.call(() => { })[object Function]
dateObject.prototype.toString.call(new Date())[object Date]
regexpObject.prototype.toString.call(/1/)[object RegExp]
symbolObject.prototype.toString.call(Symbol(1))[object Symbol]
bigintObject.prototype.toString.call(BigInt(1))[object BigInt]
mapObject.prototype.toString.call(new Map())[object Map]
setObject.prototype.toString.call(new Set())[object Set]