我们知道判断基本数据类型可以通过typeoftypeof 2 //’number’typeof ‘32’ //‘string’typeof true //“boolean"typeof undefined //“undefined"let y=Symbol(’s’)typeof y //“symbol”//判断nulltypeof null//object但是判断对象,不能用typeoflet obj={}typeof obj //“object"let arr=[]typeof arr//“object"那我们用什么方法能判断是对象还是数组呢?Object.prototype.toString 为什么能判断类型呢Object.prototype.toString.call(obj)//"[object Object]“Object.prototype.toString.call(arr)//"[object Array]“Object.prototype.toString.call(null) //"[object Null]“Object.prototype.toString.call(/test/)//"[object RegExp]“toString每个对象都有一个toString()方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。默认情况下,toString()方法被每个Object对象继承。如果此方法在自定义对象中未被覆盖,toString() 返回 “[object type]",其中type是对象的类型Object.toString.call(obj)VM2372:1 Uncaught TypeError: Function.prototype.toString requires that ’this’ be a Function at Object.toString (<anonymous>) at <anonymous>:1:17 那为什么Objet.toString 会报错呢,原因是Object是构造函数,本身是没有toString的方法的,实际调用的是Function.prototype.toString.call(param) ,这里需要的参数类型是函数,但是传了对象,所以会报错。那还有哪些判断数组的方式呢let arr=[]arr instanceof Array //true arr instanceof Object //true 这个也有弊端,就是没有办法区分是数组还是对象arr.constructor === Object //false arr.constructor===Array //trueArray.isArray 综上,Object.prototype.toString.call(params) 最靠谱