js 数据类型的判断方法

107 阅读2分钟

1、 typeof 可以区分(除 null 以外)基本类型 ,是不能区分 null 和 引用类型

typeof '1' // 'string'
typeof 1 // 'number'
typeof null // 'object'
typeof undefined // 'undefined'
typeof true // 'boolean'
typeof Symbol('1') // 'symbol'
typeof 1n // 'bigint'
typeof new Object(); // 'object'
typeof new Number(1); // 'object'
typeof new Function(); // 'function'

2、instanceof 是用来判断 A 是否为 B 的实例, 表达式为:A instanceof B,如果 A 是 B 的实例,则返回 true, 否则返回 false。 在这里需要特别注意的是:instanceof 检测的是原型

实现 instanceof
function myInstanceof(A, B){
  if (A.__proto__ === B.prototype) {
    // A的内部属性 __proto__ 指向 B 的原型对象
    return true;
  }
  return false;
}

instanceof 使用方式
[] instanceof Array; // true
[] instanceof Object; // true
function Person() {};
const person = new Person();
person instanceof Person; // true
person instanceof Object; // true

3、constructor 本来是原型对象上的属性,指向构造函数

注意:nullundefined 没有 constructor

4、Object.prototype.toString 去判断数据类型

Object.prototype.toString.call('1') // "[object String]"
Object.prototype.toString.call(1) // "[object Number]"
Object.prototype.toString.call([1]) // "[object Array]"
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(new Function()); // "[object Function]"
Object.prototype.toString.call(new Date()); // "[object Date]"
Object.prototype.toString.call(new RegExp()); // "[object RegExp]"
Object.prototype.toString.call(new Error()); // "[object Error]"

5、封装一些基本方法去判断指定数据类型

/**
 * @desc 数据类型检测
 * @param obj 待检测的数据
 * @return {String} 类型字符串
 */

function isType(obj) {
  return typeof obj !== "object" ? typeof obj : Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}


/**
 * @desc 是否是 Undefined 类型检测
 * @param obj 待检测的数据
 * @return {Boolean} 布尔值
 */
 function isUndefined(obj) {
  return obj === void 0;
}
/**
* @desc 是否是 Null 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isNull(obj) {
  return obj === null;
}
/**
* @desc 是否是 Boolean 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isBoolean(obj) {
  return typeof(obj) === 'boolean';
}
/**
* @desc 是否是 Number 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isNumber(obj) {
  return typeof(obj) === 'number';
}
/**
* @desc 是否是 String 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isString(obj) {
  return typeof(obj) === 'string';
}
/**
* @desc 是否是 Object 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isObject(obj) {
  return Object.prototype.toString.call(obj) === '[object Object]';
}
/**
* @desc 是否是 Array 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isArray(obj){
  return Array.isArray?Array.isArray(obj):Object.prototype.toString.call(obj) === '[object Array]';
}
/**
* @desc 是否是 Function 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isFunction(obj){
  return typeof(obj) === 'function';
}
/**
* @desc 是否是 Date 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isDate(obj){
  return Object.prototype.toString.call(obj) === '[object Date]';
}
/**
* @desc 是否是 RegExp 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isRegExp(obj){
  return Object.prototype.toString.call(obj) === '[object RegExp]';
}
/**
* @desc 是否是 Error 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isError(obj){
  return Object.prototype.toString.call(obj) === '[object Error]';
}
/**
* @desc 是否是 Arguments 类型检测
* @param obj 待检测的数据
* @return {Boolean} 布尔值
*/
function isArguments(obj){
  return Object.prototype.toString.call(obj) === '[object Arguments]';
}