基本类型:String、Number、Boolean、Symbol、Undefined、Null
引用类型:Object

typeof

对一个值使用 typeof 操作符会返回下列字符串之一:
●”number“ 表示值为数值;
●”boolean“ 表示值为布尔值;
●”string“ 表示值为字符串;
●”symbol“ 表示值为符号、
●”undefined“ 表示值未定义;
●”object” 表示值为对象:除 function 以外的对象都会被识别成 object;另外 null 也会识别成 object(历史包袱
●”function“ 表示值为函数;

  1. typeof ''; // string 有效
  2. typeof 1; // number 有效
  3. typeof Symbol(); // symbol 有效
  4. typeof true; //boolean 有效
  5. typeof undefined; //undefined 有效
  6. typeof null; //object 无效
  7. typeof [] ; //object 无效
  8. typeof new Function(); // function 有效
  9. typeof new Date(); //object 无效
  10. typeof new RegExp(); //object 无效

instanceof

instanceof 是用来判断 A 是否为 B 的实例

两个方法的差异性:

  • instanceof 可以准确地判断复杂引用数据类型,但是不能正确判断基础数据类型;
  • typeof 也存在弊端,它虽然可以判断基础数据类型(null 除外),但是引用数据类型中,除了 function 类型以外,其他的也无法判断。

constructor

undefined和null没有contructor属性

  1. console.log(bool.constructor === Boolean);// true
  2. console.log(num.constructor === Number);// true
  3. console.log(str.constructor === String);// true
  4. console.log(arr.constructor === Array);// true
  5. console.log(obj.constructor === Object);// true
  6. console.log(fun.constructor === Function);// true
  7. console.log(Symbol.constructor === Symbol);// true
  8. console.log(haoxl.constructor === Student);// false
  9. console.log(haoxl.constructor === Person);// true

constructor有两个作用,一是判断数据的类型,二是对象实例通过 constructor 对象访问它的构造函数。需要注意,如果创建一个对象来改变它的原型,constructor就不能用来判断数据类型了:

  1. function Fn(){};
  2. Fn.prototype = new Array();
  3. var f = new Fn();
  4. console.log(f.constructor===Fn); // false
  5. console.log(f.constructor===Array); // true

Object.prototype.toString.call()

需要注意的是,但是它不能检测非原生构造函数的构造函数名。

不同数据类型的 Object.prototype.toString 方法返回值如下。

  • 数值:返回 [object Number]。
  • 字符串:返回 [object String]。
  • 布尔值:返回 [object Boolean]。
  • undefined:返回 [object Undefined]。
  • null:返回 [object Null]。
  • 数组:返回 [object Array]。
  • arguments 对象:返回 [object Arguments]。
  • 函数:返回 [object Function]。
  • Error 对象:返回 [object Error]。
  • Date 对象:返回 [object Date]。
  • RegExp 对象:返回 [object RegExp]。
  • 其他对象:返回 [object Object]。 ```javascript function foo(){};

Object.prototype.toString.call(1); ‘[object Number]’

Object.prototype.toString.call(NaN); ‘[object Number]’

Object.prototype.toString.call(‘1’); ‘[object String]’

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(Symbol());’[object Symbol]’

Object.prototype.toString.call(foo); ‘[object Function]’

Object.prototype.toString.call([1,2,3]); ‘[object Array]’

Object.prototype.toString.call({});’[object Object]’

  1. ```javascript
  2. // 可以包装一下,判断数据类型函数
  3. function toRawType (value) {
  4. return Object.prototype.toString.call(value).slice(8, -1)
  5. }
  6. toRawType({}) // Object

image.png

https://blog.csdn.net/lhjuejiang/article/details/79623973

常用方法

https://zhuanlan.zhihu.com/p/129642585

  • 判断数组: Array.isArray()

  • 确定一个值是否为 NaN:isNaN()Number.isNaN

    isNaN() 只要不是number就会返回 true,Number.isNaN 更准确,只判断NaN为true