屬性的可列舉性和所有權

JavaScript 物件中的每個屬性都可以透過三個因素進行分類

  • 可列舉或不可列舉;
  • 字串或符號
  • 自有屬性或從原型鏈繼承的屬性。

可列舉屬性是指其內部可列舉標誌設定為 true 的屬性,這是透過簡單賦值或屬性初始化器建立的屬性的預設值。透過Object.defineProperty等方法定義的屬性預設情況下是不可列舉的。大多數迭代方式(例如for...in迴圈和Object.keys)只訪問可列舉的鍵。

屬性的所有權取決於該屬性是直接屬於物件,而不是屬於其原型鏈。

所有屬性,無論是可列舉的還是不可列舉的,字串還是符號,自有屬性還是繼承屬性,都可以透過點表示法或方括號表示法訪問。在本節中,我們將重點介紹 JavaScript 提供的逐一訪問一組物件屬性的方法。

查詢物件屬性

有四種內建方法可以查詢物件的屬性。它們都支援字串和符號鍵。下表總結了每種方法返回true的情況。

可列舉,自有 可列舉,繼承 不可列舉,自有 不可列舉,繼承
propertyIsEnumerable() true ✅ false ❌ false ❌ false ❌
hasOwnProperty() true ✅ false ❌ true ✅ false ❌
Object.hasOwn() true ✅ false ❌ true ✅ false ❌
in true ✅ true ✅ true ✅ true ✅

遍歷物件屬性

JavaScript 中有許多方法可以遍歷物件的一組屬性。有時,這些屬性作為陣列返回;有時,它們在迴圈中逐一迭代;有時,它們用於構建或修改另一個物件。下表總結了何時可以訪問屬性。

僅訪問字串屬性或僅訪問符號屬性的方法將有一個額外註釋。✅ 表示將訪問此型別的屬性;❌ 表示不會。

可列舉,自有 可列舉,繼承 不可列舉,自有 不可列舉,繼承
Object.keys
Object.values
Object.entries

(字串)
Object.getOwnPropertyNames
(字串)

(字串)
Object.getOwnPropertySymbols
(符號)

(符號)
Object.getOwnPropertyDescriptors
Reflect.ownKeys
for...in
(字串)

(字串)
Object.assign
(第一個引數之後)
物件擴充套件

根據可列舉性/所有權獲取屬性

請注意,這並非所有情況下最有效的演算法,但對於快速演示很有用。

  • 檢測可以透過SimplePropertyRetriever.theGetMethodYouWant(obj).includes(prop)進行
  • 迭代可以透過SimplePropertyRetriever.theGetMethodYouWant(obj).forEach((value, prop) => {});進行(或使用filter()map()等)
js
const SimplePropertyRetriever = {
  getOwnEnumProps(obj) {
    return this._getPropertyNames(obj, true, false, this._enumerable);
    // Or could use for...in filtered with Object.hasOwn or just this: return Object.keys(obj);
  },
  getOwnNonEnumProps(obj) {
    return this._getPropertyNames(obj, true, false, this._notEnumerable);
  },
  getOwnProps(obj) {
    return this._getPropertyNames(
      obj,
      true,
      false,
      this._enumerableAndNotEnumerable,
    );
    // Or just use: return Object.getOwnPropertyNames(obj);
  },
  getPrototypeEnumProps(obj) {
    return this._getPropertyNames(obj, false, true, this._enumerable);
  },
  getPrototypeNonEnumProps(obj) {
    return this._getPropertyNames(obj, false, true, this._notEnumerable);
  },
  getPrototypeProps(obj) {
    return this._getPropertyNames(
      obj,
      false,
      true,
      this._enumerableAndNotEnumerable,
    );
  },
  getOwnAndPrototypeEnumProps(obj) {
    return this._getPropertyNames(obj, true, true, this._enumerable);
    // Or could use unfiltered for...in
  },
  getOwnAndPrototypeNonEnumProps(obj) {
    return this._getPropertyNames(obj, true, true, this._notEnumerable);
  },
  getOwnAndPrototypeEnumAndNonEnumProps(obj) {
    return this._getPropertyNames(
      obj,
      true,
      true,
      this._enumerableAndNotEnumerable,
    );
  },
  // Private static property checker callbacks
  _enumerable(obj, prop) {
    return Object.prototype.propertyIsEnumerable.call(obj, prop);
  },
  _notEnumerable(obj, prop) {
    return !Object.prototype.propertyIsEnumerable.call(obj, prop);
  },
  _enumerableAndNotEnumerable(obj, prop) {
    return true;
  },
  // Inspired by http://stackoverflow.com/a/8024294/271577
  _getPropertyNames(obj, iterateSelf, iteratePrototype, shouldInclude) {
    const props = [];
    do {
      if (iterateSelf) {
        Object.getOwnPropertyNames(obj).forEach((prop) => {
          if (props.indexOf(prop) === -1 && shouldInclude(obj, prop)) {
            props.push(prop);
          }
        });
      }
      if (!iteratePrototype) {
        break;
      }
      iterateSelf = true;
      obj = Object.getPrototypeOf(obj);
    } while (obj);
    return props;
  },
};

另見