首页 > 解决方案 > 如何从对象中删除原型字段?

问题描述

除了通过此方法之外,我如何从对象中删除原型字段?

const input = {foo: 'bar', __proto__: {unwanted: 'things'}}
expect(JSON.parse(JSON.stringify(input))).toEqual({foo: 'bar'})  // true
// this works but is there a cleaner way ?

标签: javascriptjsonecmascript-6

解决方案


这取决于您要实现的目标,但我通常建议.hasOwnProperty检查该字段是否为原型字段

参考:MDN


推荐阅读