JS中数据类型
基本数据类型:栈内存存储,原始类型是不能携带属性和方法的
数字(number)、字符串(string)、布尔值(boolean)、undefined、null、bigint、symbol
String、Number、Boolean、Symbol、Bigint、Undefined、Null
引用数据类型:堆内存存储,引用地址还是在栈内存上
对象(Object)、数组(Array)、函数(Function)、正则(RegExp)、日期(Date)、Error等
判断数据类型的几种方式
- 使用
typeof判断 - 使用
instanceof判断 - 使用
constructor判断 - 使用
Object.prototype.toString.call()判断
使用 typeof 判断
typeof用以获取一个变量或者表达式的类型-一般用于判断值类型
typeof 123 => 'number'
typeof '123' => 'string'
typeof true => 'boolean'
typeof Symbol(1) => 'symbol'
typeof BigInt('43243') => 'bigint'
typeof null => 'object'
typeof undefined => 'undefined'
typeof {} => 'object'
typeof [] => 'object'
typeof function () {} => 'function'
typeof Math => 'object'
typeof new Date() => 'object'
typeof new RegExp() => 'object'
typeof new Error() => 'object'
typeof判断结果返回一个表示数据类型的字符串
返回结果:number、string、boolean、symbol、bigint、undefined、function、object
typeof可以测试出number、string、boolean、symbol、bigint、undefined及function- 对于
null、Object、Array、Function、RegExp、Date、Error,部分内置对象,typeof均检测出为object
使用 instanceof 判断(判断引用类型有意义)
instanceof运算符是用来判断一个对象是否在其原型链原型构造函数的属性
123 instanceof Number => false
'123' instanceof String => false
true instanceof Boolean => false
Symbol(1) instanceof Symbol => false
BigInt('43243') instanceof BigInt => false
undefined instanceof Object => false
null instanceof Object => false
[] instanceof Array => true
[] instanceof Object => true
const fn = () => {};
fn instanceof Function => true
new Date() instanceof Object => true
new RegExp() instanceof Object => true
new Error() instanceof Object => true
Math instanceof Object => true
new Number() instanceof Numbert => true
new String() instanceof Stringt => true
new Boolean() instanceof Boolean => true
new RegExp() instanceof RegExp => true
new Error() instanceof Date => true
new Date() instanceof Date => true
instanceof不能判断基本数据类型,可以判断引用类型instanceof不能区别undefined和null- 使用
new声明的类型可以检测出多层继承关系
注意
空对象 `{}` 的判断问题
{} instanceof Object => Error
Object.create({}) instanceof Object => true
Object.create(null) instanceof Object => false
使用 constructor 判断
true.constructor === Boolean => true
(111).constructor === Number => true
'123'.constructor === String => true
Symbol(1).constructor === Symbol => true
BigInt('43243').constructor === BigInt => true
[].constructor === Array => true
const obj = {
name: '张'
}
obj.constructor === Object => true
const fn = () => {};
fn.constructor === Function => true
new RegExp().constructor === RegExp => true
new RegExp() instanceof RegExp => true
new Error().constructor === Error => true
new Date().constructor === Date => true
注意:
空对象 `{}` 的判断问题
{}.constructor === Object => Error
undefined、null没有contructor属性constructor在类继承时会出错,contructor的指向是可以改变的- 判断数字时使用(),否则会报错
使用 Object.prototype.toString.call() 判断
- 在任何值上调用 Object 原生的 toString() 方法,都会返回一个 [object NativeConstructorName] 格式的字符串。
- 每个类在内部都有一个 [[Class]] 属性,这个属性中就指定了上述字符串中的构造函数名。
- 但是它不能检测非原生构造函数的构造函数名。
Object.prototype.toString.call('') => '[object String]'
Object.prototype.toString.call(1) => '[object Number]'
Object.prototype.toString.call(true) => '[object Boolean]'
Object.prototype.toString.call(Symbol(1)) => '[object Symbol]'
Object.prototype.toString.call(BigInt('43243')) => '[object BigInt]'
Object.prototype.toString.call(undefined) => '[object Undefined]'
Object.prototype.toString.call(null) => '[object Null]'
Object.prototype.toString.call({}) => '[object Object]'
Object.prototype.toString.call([]) => '[object Array]'
Object.prototype.toString.call(new Array()) => '[object Array]'
Object.prototype.toString.call(new Function()) => '[object Function]'
Object.prototype.toString.call(new Date()) => '[object Date]'
Object.prototype.toString.call(new RegExp()) => '[object RegExp]'
Object.prototype.toString.call(new Error()) => '[object Error]'
Object.prototype.toString.call(document) => '[object HTMLDocument]'
Object.prototype.toString.call(window) => '[object Window]'