Object.hasOwn()

Baseline 已广泛支持

此特性已经十分成熟,可在许多设备和浏览器版本上使用。自 2022 年 3 月起,它已在各浏览器中可用。

Object.hasOwn() 静态方法当指定的对象拥有指示的属性作为其自身属性时,返回 true。如果属性是继承的,或者不存在,该方法返回 false

注意: Object.hasOwn() 是为了取代 Object.prototype.hasOwnProperty() 而设计的。

试一试

const object = {
  prop: "exists",
};

console.log(Object.hasOwn(object, "prop"));
// Expected output: true

console.log(Object.hasOwn(object, "toString"));
// Expected output: false

console.log(Object.hasOwn(object, "undeclaredPropertyValue"));
// Expected output: false

语法

js
Object.hasOwn(obj, prop)

参数

obj

要测试的 JavaScript 对象实例。

prop

要测试的属性的 String 名称或 Symbol

返回值

如果指定的对象直接定义了指定的属性,则为 true。否则为 false

描述

Object.hasOwn() 方法当指定的属性是对象的直接属性时返回 true — 即使属性值是 nullundefined。如果属性是继承的,或者根本没有声明,则该方法返回 false。与 in 运算符不同,此方法不检查对象原型链中的指定属性。

建议使用它而不是 Object.prototype.hasOwnProperty(),因为它适用于 null-prototype 对象,并且可以与已覆盖继承的 hasOwnProperty() 方法的对象一起使用。虽然可以通过在另一个对象上访问 Object.prototype.hasOwnProperty() 来解决这些问题(例如 Object.prototype.hasOwnProperty.call(obj, prop)),但 Object.hasOwn() 更直观、更简洁。

示例

使用 Object.hasOwn() 测试属性是否存在

以下代码演示了如何确定 example 对象是否包含名为 prop 的属性。

js
const example = {};
Object.hasOwn(example, "prop"); // false - 'prop' has not been defined

example.prop = "exists";
Object.hasOwn(example, "prop"); // true - 'prop' has been defined

example.prop = null;
Object.hasOwn(example, "prop"); // true - own property exists with value of null

example.prop = undefined;
Object.hasOwn(example, "prop"); // true - own property exists with value of undefined

直接属性与继承属性

以下示例区分了直接属性和通过原型链继承的属性。

js
const example = {};
example.prop = "exists";

// `hasOwn` will only return true for direct properties:
Object.hasOwn(example, "prop"); // true
Object.hasOwn(example, "toString"); // false
Object.hasOwn(example, "hasOwnProperty"); // false

// The `in` operator will return true for direct or inherited properties:
"prop" in example; // true
"toString" in example; // true
"hasOwnProperty" in example; // true

迭代对象的属性

要迭代对象的枚举属性,您应该使用

js
const example = { foo: true, bar: true };
for (const name of Object.keys(example)) {
  // …
}

但是,如果您需要使用 for...in,您可以使用 Object.hasOwn() 来跳过继承的属性。

js
const example = { foo: true, bar: true };
for (const name in example) {
  if (Object.hasOwn(example, name)) {
    // …
  }
}

检查数组索引是否存在

数组的元素被定义为直接属性,因此您可以使用 hasOwn() 方法来检查特定索引是否存在。

js
const fruits = ["Apple", "Banana", "Watermelon", "Orange"];
Object.hasOwn(fruits, 3); // true ('Orange')
Object.hasOwn(fruits, 4); // false - not defined

hasOwnProperty() 的有问题情况

本节演示了 Object.hasOwn() 不受 hasOwnProperty() 问题的影响。首先,它可以与重新实现 hasOwnProperty() 的对象一起使用。在下面的示例中,重新实现的 hasOwnProperty() 方法报告所有属性均为 false,但 Object.hasOwn() 的行为保持不变。

js
const foo = {
  hasOwnProperty() {
    return false;
  },
  bar: "The dragons be out of office",
};

console.log(foo.hasOwnProperty("bar")); // false

console.log(Object.hasOwn(foo, "bar")); // true

它也可以与 null-prototype 对象一起使用。这些对象不继承自 Object.prototype,因此 hasOwnProperty() 不可用。

js
const foo = Object.create(null);
foo.prop = "exists";

console.log(foo.hasOwnProperty("prop"));
// Uncaught TypeError: foo.hasOwnProperty is not a function

console.log(Object.hasOwn(foo, "prop")); // true

规范

规范
ECMAScript® 2026 语言规范
# sec-object.hasown

浏览器兼容性

另见