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

瀏覽器相容性

另見