美文网首页
js超具体的类型判断方法

js超具体的类型判断方法

作者: Adder | 来源:发表于2021-05-06 18:16 被阅读0次
const objectTypes = {
  null: '[object Null]',
  undefined: '[object Undefined]',
  regExp: '[object RegExp]',
  number: '[object Number]',
  string: '[object String]',
  function: '[object Function]',
  symbol: '[object Symbol]',
  object: '[object Object]',
  date: '[object Date]',
  math: '[object Math]',
  window: '[object Window]',
};

/**
 * 判断值是否为对象
 * @param {object} value 值
 */
export const isObject = (value: any): boolean => {
  if (value === undefined || value === null) return false;
  if (Object.prototype.toString.apply(value) === objectTypes.object) {
    return true;
  }
  return false;
};

export const isString = (value: any): boolean => {
  if (value === undefined || value === null) return false;
  if (Object.prototype.toString.apply(value) === objectTypes.string) return true;
  return false;
};

相关文章

网友评论

      本文标题:js超具体的类型判断方法

      本文链接:https://www.haomeiwen.com/subject/jliddltx.html