Use cases for Object.create(null)

前端 未结 2 878
终归单人心
终归单人心 2020-12-06 05:52

I understand that using Object.create(null) creates an object which has no proto property (i.e. Object.getPrototypeOf( myObj ) === null

2条回答
  •  春和景丽
    2020-12-06 06:17

    You can use such an object for a key-value map. Without the prototype chain, you can be sure that things like .toString() do not unwittingly exist on the object. That means you can incautiously access properties on it, and you can use the in operator instead of Object.prototype.hasOwnProperty.call.

提交回复
热议问题