JS 数据类型

65 阅读2分钟

最新的 ECMAScript 标准定义了 9 种数据类型:

    • undefinedtypeof instance === "undefined"
    • Booleantypeof instance === "boolean"
    • Numbertypeof instance === "number"
    • Stringtypeof instance === "string
    • BigInttypeof instance === "bigint"
    • Symboltypeof instance === "symbol"
  • nulltypeof instance === "object"
  • Objecttypeof instance === "object"。任何 constructed 对象实例的特殊非数据结构类型,也用做数据结构:new Object,new Array,new Map,new Set,new WeakMap,new WeakSet,new Date,和几乎所有通过 new keyword 创建的东西。
  • Function:非数据结构,尽管 typeof 操作的结果是:typeof instance === "function"。这个结果是为 Function 的一个特殊缩写,尽管每个 Function 构造器都由 Object 构造器派生。

1、基本数据类型

Boolean、Number、Symbol、String、Null、Undefined、(bigInt)

( bigInt。是指安全存储、操作大整数。(但是很多人不把这个做为一个类型))

(Symbol 本质上是一种唯一标识符,可用作对象的唯一属性名,这样其他人就不会改写或覆盖你设置的属性值。)

2、引用类型

Object

Object 包含( function、Array、Date)

3、类型判断

(1)typeof

  typeof运算符的返回类型为字符串,值包括如下几种:

        1. 'undefined'            --未定义的变量或值 console.log(typeof a);  console.log(typeof undefined); 

        2. 'boolean'              --布尔类型的变量或值 console.log(typeof(true));

        3. 'string'                 --字符串类型的变量或值 console.log(typeof '123');

        4. 'number'              --数字类型的变量或值  console.log(typeof 123)

        5. 'object'                --对象类型的变量或值,或者null(这个是js历史遗留问题,将null作为object类型处理)

        6. 'function'               --函数类型的变量或值   var  fn = function(){}; console.log(typeof(fn))

特殊的:

typeof 23423n; // bigint

let a = Symbol();

typeof a; // symbol

(2)instanceof

instanceof操作符判断左操作数对象的原型链上是否有右边这个构造函数的prototype属性,也就是说指定对象是否是某个构造函数的实例,最后返回布尔值。

instanceof (A,B) = {
    var L = A.__proto__;
    var R = B.prototype;
    if(L === R) {
        //A的内部属性__proto__指向B的原型对象
        return true;
    }
    return false;
}
[] instanceof Array//true
[] instanceof Object//true
new Date() instanceof Date;//true
new Date() instanceof Object;//true
function Person(){};
new Person() instanceof Person;//true
new Person() instanceof Object;//true

注意:instanceof运算符只能用于对象,不适用原始类型的值。

//

'hello' instanceof String // false
null instanceof Object // false
undefined instanceof Object // false

null 不是对象

(3)constructor

     得知某个实例对象 到底是由那个构造函数产生

但是 constructor 属性易变,不可信赖,这个主要体现在自定义对象上,当开发者重写prototype后,原有的constructor会丢失

不推荐使用

(4)Object.prototype.toString 

Object.prototype.toString.call('') ;   // [object String]
Object.prototype.toString.call(1) ;    // [object Number]
Object.prototype.toString.call(true) ; // [object Boolean]
Object.prototype.toString.call(undefined) ; // [object Undefined]
Object.prototype.toString.call(null) ; // [object Null]
Object.prototype.toString.call(new Function()) ; // [object Function]
Object.prototype.toString.call(new Date()) ; // [object Date]
Object.prototype.toString.call([]) ; // [object Array]
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]

较为复杂不推荐使用