推荐使用 Object.hasOwn:
const object1 = { prop: 'exists', }; console.log(Object.hasOwn(object1, 'prop')); // Expected output: true
如果使用 TypeScript,需要在 tsconfig.json 中注明:
"lib": [ES2022]
参考:MDN文档 Object.hasOwn()
京公网安备 11010502049817号