typeof 运算符

用来判断类型的话,首先想到 typeof 运算符,具体是怎样个情况看 mdn 的解释。

typeof 操作符返回一个字符串,表示未经计算的操作数的类型。

不是很看得明白是干什么的,那我们做实验吧。

  1. console.log(typeof 1); // number
  2. console.log(typeof "string"); // string
  3. console.log(typeof true); // boolean
  4. console.log(typeof undefined); // undefined

特殊情况

  1. const nul = null;
  2. const obj = { a: 1 };
  3. const array = [1, 2, 3];
  4. const date = new Date();
  5. const error = new Error();
  6. const reg = /a/g;
  7. const func = function a() {};
  8. console.log(typeof nul); // object
  9. console.log(typeof obj); // object
  10. console.log(typeof array); // object
  11. console.log(typeof date); // object
  12. console.log(typeof error); // object
  13. console.log(typeof reg); // object
  14. console.log(typeof func); // function

从上面可以知道 null 会返回 object (很奇怪),其他不管是什么对象都返回 object,但函数却能判断出来返回 function。

typeof 只能判断 6 种类型,那有没有其他办法了,可以判断更多的情况了?答案就是 Object.prototype.toString 方法。

Object.prototype.toString

先来看一下 mdn 是怎样介绍这个方法的。

每个对象都有一个 toString() 方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。默认情况下, toString() 方法被每个 Object 对象继承。如果此方法在自定义对象中未被覆盖, toString() 返回 "[object type]" ,其中 type 是对象的类型。

也还是看不懂那就做实验吧。

  1. console.log(Object.prototype.toString.call(undefined)); // [object Undefined]
  2. console.log(Object.prototype.toString.call(null)); // [object Null]
  3. const date = new Date();
  4. console.log(Object.prototype.toString.call(date)); // [object Date]

结合例子很容易知道,mdn 的那段话中的 type 就是类型。

那 Object.prototype.toString 方法可以判断多少种类型了?还是做实验。

  1. // 以下是11种:
  2. var number = 1; // [object Number]
  3. var string = "123"; // [object String]
  4. var boolean = true; // [object Boolean]
  5. var und = undefined; // [object Undefined]
  6. var nul = null; // [object Null]
  7. var obj = { a: 1 }; // [object Object]
  8. var array = [1, 2, 3]; // [object Array]
  9. var date = new Date(); // [object Date]
  10. var error = new Error(); // [object Error]
  11. var reg = /a/g; // [object RegExp]
  12. var func = function a() {}; // [object Function]
  13. function checkType() {
  14. for (var i = 0; i < arguments.length; i++) {
  15. console.log(Object.prototype.toString.call(arguments[i]));
  16. }
  17. }
  18. checkType(number, string, boolean, und, nul, obj, array, date, error, reg, func);

还有

  1. // 以下是2种
  2. console.log(Object.prototype.toString.call(Math)); // [object Math]
  3. console.log(Object.prototype.toString.call(JSON)); // [object JSON]

还有

  1. // 以下是1种
  2. function a() {
  3. console.log(Object.prototype.toString.call(arguments)); // [object Arguments]
  4. }
  5. a();

既然有 14 种之多!!!

type API

根据上面介绍到的两种方法,我们可以写出一个 type 函数来。

  1. // 类型映射
  2. const classToType = {};
  3. "Boolean Number String Function Array Date RegExp Object Error Null Undefined".split(" ").map((item, index) => {
  4. classToType["[object " + item + "]"] = item.toLowerCase();
  5. });
  6. function type(obj) {
  7. // 兼容 ie6,因为在 ie6 环境下 对 null、undefined 执行 Object.prototype.toString() 会返回 [object Object]
  8. // null == undefined // true(神奇不)
  9. if (obj == null) {
  10. return obj + "";
  11. }
  12. return typeof obj === "object" || typeof obj === "function"
  13. ? classToType[Object.prototype.toString.call(obj)] || "object"
  14. : typeof obj;
  15. }

利用 type 函数封装其他

isFunction 函数

  1. function isFunction(obj) {
  2. return type(obj) === "function";
  3. }

array

  1. var isArray = Array.isArray || function( obj ) {
  2. return type(obj) === "array";
  3. }

参考:

[1] Javascript 专题之类型判断(上)