← 返回完整题库
JavaScript简单

hasOwnProperty 与 instanceof 有什么区别

参考答案

  • obj.hasOwnProperty(key):判断属性是否为对象自有(不含原型链),返回布尔;
  • obj instanceof Ctor:判断构造函数的 prototype 是否出现在对象的原型链上,用于类型/继承关系判断。
const a = [];
'a'.length;                    // 自有
a instanceof Array;            // true(原型链判断)

替代:Object.hasOwn(obj, k);类型判断更可靠的是 Array.isArray、Object.prototype.toString。

本题来自 计算机知识面试题库,共 921 道题。