js中的数据类型 和 判断数据类型的方法

104 阅读2分钟

一、数据类型

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

1. 七个基本数据类型:

  • 布尔值(Boolean) ,有 2 个值分别是:true 和 false.
  • null,一个表明 null 值的特殊关键字。JavaScript 是大小写敏感的,因此 null 与 Null、NULL或变体完全不同。
  • undefined,和 null 一样是一个特殊的关键字,undefined 表示变量未赋值时的属性。
  • 数字(Number) ,整数或浮点数,例如: 42 或者 3.141592653589。
  • 字符串(String) ,字符串是一串表示文本值的字符序列,例如:"hello word" 。
  • 代表(Symbol) ( 在 ECMAScript 6 中新添加的类型).。一种实例是唯一且不可改变的数据类型。
  • 任意精度的整数 (BigInt) ,它提供了一种方法来表示大于 2^53 - 1 的整数。这原本是 Javascript 中可以用 Number 表示的最大数字。BigInt 可以表示任意大的整数。可以用在一个整数字面量后面加 n 的方式定义一个 BigInt如:10n,或者调用函数 BigInt()(但不包含 new 运算符)并传递一个整数值或字符串值。

2. 引用类型

  • 对象(Object) ,包括 Object Array、Function ,Date,RegExp 等
// 基本数据类型
let theString = "lucky";
let theNumber = 998;
let theBoolean = true;
let theNull = null;
let theUndefined = undefined;
let theSymbol = Symbol(""); // es6新增 值唯一
let theBiggestInt = 9007199254740991n;
​
/** 引用数据类型 */
const theObject = {
  obj: { name: "嬴政" },
  date: new Date(), //日期
  func: () => console.log("我是箭头函数"), // func
  arr: ["我", "是", "数", "组"], // array
  reg: /^[0-9]+(.[0-9]{2})?$/g, // 正则
};

3. 两者区别

  • 基本类型数据放在栈内存
  • 引用类型以:地址: 数据 的映射关系来进行存储,其中地址放在栈内存,数据放在堆内存,若两个或 N 个变量指向同一个地址,则共用一份数据。
  • const 定义的 基本数据类型的值 是不能修改的 引用数据类型 则可以修改

二、判断数据类型(只列出了常用的)

1. typeof

  const { log } = console;
  log(typeof "lucky"); //--------> string
  log(typeof 998); //------------> number
  log(typeof true); //-----------> boolean
  log(typeof NaN); //------------> number
  log(typeof null); //-----------> object
  log(typeof undefined); //------> undefined
  log(typeof Symbol("1")); //----> symbol
  log(typeof 900719920n); //-----> bigint
  log(typeof []); //-------------> object
  log(typeof {}); //-------------> object
  log(typeof new Date()); //-----> object
  log(typeof new RegExp()); //---> object
  log(typeof new Function()); //-> function

2. instanceof

  • 语法:obj instanceof Type
  • 用于检测构造函数的prototype属性是否出现在某个实例对象的原型链上
log("lucky" instanceof String); // true
log([] instanceof Array); // true
log([] instanceof Object); // true
log({} instanceof Object); // true
log({} instanceof Array); // false
function Persion(name, age, happy = []) {
  name, age, happy;
}
const zs = new Persion("张三", 18);
log(zs instanceof Persion); // true

3. constructor

  • constructor 指向创建该实例对象的构造函数
  • null和undefined没有constructor;
  • 以及 constructor 可以被改写
log("lucky".constructor == String); // true
log(false.constructor == Boolean); // true
log((123).constructor == Number); // true
const arr = [];
const obj = {};
log(arr.constructor == Array); // true
log(arr.constructor == Object);// false
log(obj.constructor == Array); // false
log(obj.constructor == Object);// true
​
​
// 修改了 constructor
function fn() {}
log("lucky".constructor); // String
String.prototype.constructor = fn;
log("lucky".constructor); // fn(){}
log("lucky".constructor == String); // false

3. Object.prototype.toString.call()

  • 适用于所有类型的判断检测, 推荐使用
  • Object.prototype.toString.call() 返回的字符串格式为 "[object 类型]"
// 基本数据类型
let theString = "lucky";
let theNumber = 998;
let theBoolean = true;
let theNull = null;
let theUndefined = undefined;
let theSymbol = Symbol("1"); // es6新增 值唯一
let theBiggestInt = 9007199254740991n;
/** 引用数据类型 */
const theObject = {
  obj: { name: "嬴政" },
  date: new Date(), //日期
  func: () => console.log("我是箭头函数"), // func
  arr: ["我", "是", "数", "组"], // array
  reg: /^[0-9]+(.[0-9]{2})?$/g,
};
​
const { prototype: { toString } } = Object;  // 结构出来 toString
log(toString.call(theString)); // -----> [object String]
log(toString.call(theNumber)); // -----> [object Number]
log(toString.call(theBoolean)); // ----> [object Boolean]
log(toString.call(theNull)); // -------> [object Null]
log(toString.call(theSymbol)); //------> [object Symbol]
log(toString.call(theUndefined)); //---> [object Undefined]
log(toString.call(theBiggestInt)); //--> [object BigInt]
log(toString.call(theObject.obj)); // -> [object Object]
log(toString.call(theObject.arr)); // -> [object Array]
log(toString.call(theObject.func)); //-> [object Function]
log(toString.call(theObject.reg)); //--> [object RegExp]
log(toString.call(theObject.date)); //-> [object Date]
​
const arr = [  theString,  theNumber,  theBoolean,  theNull,  theUndefined,  theSymbol,  theBiggestInt,  theObject.date,  theObject.func,  theObject.arr,  theObject.reg,  theObject.obj,];
// 封装
const getType = (data) => Object.prototype.toString.call(data).slice(8, -1);
log(arr.map((item) => getType(item)));
// ['String', 'Number', 'Boolean', 'Null', 'Undefined', 'Symbol', 'BigInt', 'Date', 'Function', 'Array', 'RegExp', 'Object']

4. 严格运算符===

let a = null;
typeof a; //object
a === null; //true
/*扩展补充*/
//判断一个非数组变量是否为空
if (typeof a != "undefined" && a) {
}
//判断一个数组变量是否为空
if (typeof a != undefined && a && a.length > 0) {
}