Skip to main content

JavaScript 类型相关

1.JavaScript 数据类型

JavaScript 有八种内置类型,除对象外,其他统称为基本类型

  • 空值(null)

  • 未定义(undefined)

  • 布尔值(boolean)

  • 数字(number)

  • 字符串(string)

  • Symbol: 是 ES6 中引入的一种原始数据类型,表示独一无二的值。

  • BigInt:是 ES2020 引入的一种新的数据类型,用来解决 JavaScript 中数字只能到 53 个二进制位(JavaScript 所有数字都保存成 64 位浮点数,大于这个范围的整数,无法精确表示的问题。具体可查看:新数据类型 — BigInt 复制代码

  • 对象 (object)

2.JavaScript 类型判断

typeof 判断除(null)之外的基本数据类型 instanceof 可以判断引用数据类型 constructor 可以判断除了(null,undefined)所有类型,包括对象;易被修改,不能跨 iframe 传递 Object.prototype.toString.call(value) 可以判断所有类型

2.1.typeof

判断基本数据类型,可以判断出 null 的基本数据类型,null 被认为一个空对象引用

typeof null //'object'
typeof undefined //'undefined'
typeof true //'boolean'
typeof false //'boolean'
typeof 123 //'number'
typeof '123' //'string'
typeof Symbol('123') //'symbol'
typeof {} //'object'
typeof [] //'object'
typeof function () {} //'function'

2.2.instanceof

instanceof 是用来判断 A 是否为 B 的实例,可以判断出 null 的实例,null 被认为一个空对象引用,但是不能判断出 undefined 的实例,undefined 被认为一个未定义的值

instanceof 无法判断基本数据类型

var a = new Date()
a instanceof Date //true
a instanceof Object //true
a instanceof RegExp //false
let undef
undef instanceof Object //false
undef instanceof RegExp //false

let ary = [1, 2, 3]
ary instanceof Array //true

let obj = { a: 1 }
obj instanceof Object //true

let reg = /\d+/
reg instanceof RegExp //true
reg instanceof Object //true

let str = '123'
str instanceof String //true
str instanceof Object //true
str instanceof Number //false
str instanceof RegExp //false
str instanceof Array //false

let nul = null
nul instanceof Object //true
nul instanceof Array //false

let num = 123
num instanceof Number //true
num instanceof Object //true
num instanceof Array //false

let bool = true
bool instanceof Boolean //true
bool instanceof Object //true
function An() {}
function Bottle() {}
An.prototype = Bottle.prototype = {}

let an = new An()
console.log(an instanceof Bottle) //true

// an.__proto__ === An.prototype; // true
// An.prototype === Bottle.prototype; // true
// // 即
// an.__proto__ === Bottle.prototype; // true

2.3.Object.prototype.toString

toString()是 Object 原型的一个方法,用来返回一个对象的字符串表示,可以用来判断对象的类型

Object.prototype.toString.call(1) //"[object Number]"
Object.prototype.toString.call(true) //"[object Boolean]"
Object.prototype.toString.call('123') //"[object String]"
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(/\d+/) //"[object RegExp]"
Object.prototype.toString.call(function () {}) //"[object Function]"
Object.prototype.toString.call([]) //"[object Array]"
Object.prototype.toString.call({}) //"[object Object]"
Object.prototype.toString.call(new Error()) //"[object Error]"
Object.prototype.toString.call(new Array(1)) //"[object Array]"
Object.prototype.toString.call(new ArrayBuffer(1)) //"[object ArrayBuffer]"
Object.prototype.toString.call(new Int8Array(1)) //"[object Int8Array]"
Object.prototype.toString.call(new Uint8Array(1)) //"[object Uint8Array]"
Object.prototype.toString.call(new Uint8ClampedArray(1)) //"[object Uint8ClampedArray]"
Object.prototype.toString.call(new Int16Array(1)) //"[object Int16Array]"
Object.prototype.toString.call(new Uint16Array(1)) //"[object Uint16Array]"
Object.prototype.toString.call(new Int32Array(1)) //"[object Int32Array]"
Object.prototype.toString.call(new Uint32Array(1)) //"[object Uint32Array]"
Object.prototype.toString.call(new Float32Array(1)) //"[object Float32Array]"
Object.prototype.toString.call(new Float64Array(1)) //"[object Float64Array]"
Object.prototype.toString.call(new BigInt64Array(1)) //"[object BigInt64Array]"
Object.prototype.toString.call(new BigUint64Array(1)) //"[object BigUint64Array]"
Object.prototype.toString.call(new Map()) //"[object Map]"
Object.prototype.toString.call(new WeakMap()) //"[object WeakMap]"
Object.prototype.toString.call(new Set()) //"[object Set]"
Object.prototype.toString.call(new WeakSet()) //"[object WeakSet]"
Object.prototype.toString.call(new Promise(() => {})) //"[object Promise]"
Object.prototype.toString.call(new Proxy(() => {}, {})) //"[object Proxy]"
Object.prototype.toString.call(new SharedArrayBuffer(1)) //"[object SharedArrayBuffer]"
Object.prototype.toString.call(new DataView(new ArrayBuffer(1))) //"[object DataView]"
Object.prototype.toString.call(new ArrayBuffer(1)) //"[object ArrayBuffer]"
Object.prototype.toString.call(new Int8Array(1)) //"[object Int8Array]"
Object.prototype.toString.call(new Uint8Array(1)) //"[object Uint8Array]"
Object.prototype.toString.call(new Uint8ClampedArray(1)) //"[object Uint8ClampedArray]"
Object.prototype.toString.call(new Int16Array(1)) //"[object Int16Array]"
Object.prototype.toString.call(new Uint16Array(1)) //"[object Uint16Array]"
Object.prototype.toString.call(new Int32Array(1)) //"[object Int32Array]"
Object.prototype.toString.call(new Uint32Array(1)) //"[object Uint32Array]"
Object.prototype.toString.call(new Float32Array(1)) //"[object Float32Array]"
Object.prototype.toString.call(new Float64Array(1)) //"[object Float64Array]"
Object.prototype.toString.call(new BigInt64Array(1)) //"[object BigInt64Array]"
Object.prototype.toString.call(new BigUint64Array(1)) //"[object BigUint64Array]"

2.4.Constructor

constructor 是构造函数的引用,可以用来判断对象的类型

var a = new Number(1)
a.constructor === Number //true
a.constructor === Object //true
a.constructor === Function //true
a.constructor === Object.prototype.constructor //true
a.constructor === Number.prototype.constructor //true
a.constructor === Object.prototype.constructor.constructor //true
a.constructor === Function.prototype.constructor //true
a.constructor === Function.prototype.constructor.constructor //true

null 和 undefined 的 constructor 都是 Object

null.constructor === Object //true
undefined.constructor === Object //true

跨 iframe, 不是一个全局的 window 对象,所以不能用来判断

window.onload = function () {
var oF = document.createElement('iframe')
document.body.appendChild(oF)

var ifArray = window.frames[0].Array

var arr = new ifArray()

//alert( arr.constructor == Array ); //false

//alert( arr instanceof Array ); //false

alert(Object.prototype.toString.call(arr) == '[object Array]') //true
}