# 如何判断类对象

function isObjectLike(value) {
  return typeof value === 'object' && value !== null;
}

通过typeof操作符,返回值为object且值不为null,就认为是类对象。

因为null的typeof返回object,所以需要单独过滤,这是JS的bug,但是未修复。

另外还需要注意一点,在由宿主实现的对象中,规范规定了不能返回 undefinedbooleannumberstring 这几种类型,但是 document.all 例外,返回的是 undefined ,这是不遵循规范的实现。

最近更新时间: 2020/9/6 11:30:38