現在,撰寫類似這樣的程式碼非常普遍
const hasOwnProperty = Object.prototype.hasOwnProperty;
if (hasOwnProperty.call(object, 'foo')) {
// `object` has property `foo`.
}
或使用提供 Object.prototype.hasOwnProperty
簡易版本的函式庫,例如 has 或 lodash.has。
有了 Object.hasOwn
提案,我們可以簡單地撰寫
if (Object.hasOwn(object, 'foo')) {
// `object` has property `foo`.
}
Object.hasOwn
已在 V8 v9.3 中提供,並在 --harmony-object-has-own
旗標後方,我們將很快在 Chrome 中推出。
Object.hasOwn
支援 #
- Chrome: 支援
- Firefox: 支援
- Safari: 支援
- Node.js: 不支援
- Babel: 支援