1.判断对象类型的方法:
//万能的类型判断方法,可以判断所有对象的类型
const objectToString = Object.prototype.toString;
const toTypeString = (value) => objectToString.call(value);
//判断是否是Array
const isArray = Array.isArray;
//判断是否是Map
const isMap = (val) => toTypeString(val) === '[object Map]';
//判断是否是Set
const isSet = (val) => toTypeString(val) === '[object Set]';
//判断是否是Date
const isDate = (val) => val instanceof Date;
//判断是否是Function
const isFunction = (val) => typeof val === 'function';
//判断是否是String
const isString = (val) => typeof val === 'string';
//判断是否是Symbol
const isSymbol = (val) => typeof val === 'symbol';
//判断是否是非空对象
const isObject = (val) => val !== null && typeof val === 'object';
//判断是否是Promise
const isPromise = (val) => {
return isObject(val) && isFunction(val.then) && isFunction(val.catch);
};
//判断是否是普通的Object对象
const isPlainObject = (val) => toTypeString(val) === '[object Object]';
//特别注意:
1.typeof 对象判断方法:
typeof null // "object";
typeof undefined //"undefined"
2.声明未赋值的变量的类型为undefined:
let abc //undefined