如何判断一个变量是“纯”对象

function pureObject(o: any) {
  if (!isObject(o)) return false

  const ctor = o.constructor
  if (ctor == null) return true

  const prot = ctor.prototype
  if (!isObject(prot)) return false

  if (!Object.prototype.hasOwnProperty.call(prot, 'isPrototypeOf')) return false

  return true
}

function isObject(v: any) {
  return Object.prototype.toString.call(v) === '[object Object]'
}

参考: is-plain-object

最后更新于