Object.hasOwn

發佈於 · 標籤為 ECMAScript

現在,撰寫類似這樣的程式碼非常普遍

const hasOwnProperty = Object.prototype.hasOwnProperty;

if (hasOwnProperty.call(object, 'foo')) {
// `object` has property `foo`.
}

或使用提供 Object.prototype.hasOwnProperty 簡易版本的函式庫,例如 haslodash.has

有了 Object.hasOwn 提案,我們可以簡單地撰寫

if (Object.hasOwn(object, 'foo')) {
// `object` has property `foo`.
}

Object.hasOwn 已在 V8 v9.3 中提供,並在 --harmony-object-has-own 旗標後方,我們將很快在 Chrome 中推出。

Object.hasOwn 支援 #