Object.hasOwn()

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

¥The Object.hasOwn() static method returns true if the specified object has the indicated property as its own property. If the property is inherited, or does not exist, the method returns false.

注意:Object.hasOwn() 旨在替代 Object.prototype.hasOwnProperty()

¥Note: Object.hasOwn() is intended as a replacement for Object.prototype.hasOwnProperty().

Try it

语法

¥Syntax

js
Object.hasOwn(obj, prop)

参数

¥Parameters

obj

要测试的 JavaScript 对象实例。

prop

要测试的属性的 String 名称或 符合

返回值

¥Return value

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

¥true if the specified object has directly defined the specified property. Otherwise false

描述

¥Description

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

¥The Object.hasOwn() method returns true if the specified property is a direct property of the object — even if the property value is null or undefined. The method returns false if the property is inherited, or has not been declared at all. Unlike the in operator, this method does not check for the specified property in the object's prototype chain.

建议在 Object.prototype.hasOwnProperty() 上使用它,因为它适用于 null-原型对象 以及覆盖了继承的 hasOwnProperty() 方法的对象。虽然可以通过在外部对象上调用 Object.prototype.hasOwnProperty() 来解决这些问题,但 Object.hasOwn() 更直观。

¥It is recommended over Object.prototype.hasOwnProperty() because it works for null-prototype objects and with objects that have overridden the inherited hasOwnProperty() method. While it is possible to workaround these problems by calling Object.prototype.hasOwnProperty() on an external object, Object.hasOwn() is more intuitive.

示例

¥Examples

使用 hasOwn 测试属性是否存在

¥Using hasOwn to test for a property's existence

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

¥The following code shows how to determine whether the example object contains a property named 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

直接属性与继承属性

¥Direct vs. inherited properties

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

¥The following example differentiates between direct properties and properties inherited through the prototype chain:

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

迭代对象的属性

¥Iterating over the properties of an object

要迭代对象的可枚举属性,你应该使用:

¥To iterate over the enumerable properties of an object, you should use:

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

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

¥But if you need to use for...in, you can use Object.hasOwn() to skip the inherited properties:

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

检查数组索引是否存在

¥Checking if an Array index exists

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

¥The elements of an Array are defined as direct properties, so you can use hasOwn() method to check whether a particular index exists:

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

hasOwnProperty 的问题案例

¥Problematic cases for hasOwnProperty

本节演示 hasOwn() 不会受到影响 hasOwnProperty 的问题的影响。首先,它可以与重新实现 hasOwnProperty() 的对象一起使用:

¥This section demonstrates that hasOwn() is immune to the problems that affect hasOwnProperty. Firstly, it can be used with objects that have reimplemented hasOwnProperty():

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

if (Object.hasOwn(foo, "bar")) {
  console.log(foo.bar); // true - re-implementation of hasOwnProperty() does not affect Object
}

也可与 null-原型对象 一起使用。它们不是从 Object.prototype 继承的,因此 hasOwnProperty() 是不可访问的。

¥It can also be used with null-prototype objects. These do not inherit from Object.prototype, and so hasOwnProperty() is inaccessible.

js
const foo = Object.create(null);
foo.prop = "exists";
if (Object.hasOwn(foo, "prop")) {
  console.log(foo.prop); // true - works irrespective of how the object is created.
}

规范

Specification
ECMAScript Language Specification
# sec-object.hasown

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看