数据类型

Number、String、Boolean、Null、undefined、object、symbol、bigInt。

判断数据类型 

Object.prototype.toString.call
原理:调用了Object原型对象的tostring方法
 
typeof 运算符返回一个用来表示表达式的数据类型的字符串

typeof null   返回类型错误,返回object

引用类型,除了function返回function类型外,其他均返回object。

其中,null 有属于自己的数据类型 Null , 引用类型中的 数组、日期、正则 也都有属于自己的具体类型,而 typeof 对于这些类型的处理,只返回了处于其原型链最顶端的 Object 类型,没有错,但不是我们想要的结果。

constructor

根据对象的constructor判断,返回对创建此对象的数组函数的引用

var c= [1,2,3]; 
var d = new Date(); 
var e = function(){alert(111);}; 
alert(c.constructor === Array) ----------> true 
alert(d.constructor === Date) -----------> true 
alert(e.constructor === Function) -------> true 
//注意: constructor 在类继承时会出错

instanceof 用来比较一个对象是否为某一个构造函数的实例。注意,instanceof运算符只能用于对象,不适用原始类型的值。

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);

console.log(auto instanceof Car);
// expected output: true

console.log(auto instanceof Object);
// expected output: true

  

原文地址:https://www.cnblogs.com/studyWeb/p/14245856.html