Creating JS object with Object.create(null)?

前端 未结 5 512
不知归路
不知归路 2020-11-28 01:58

I know a lot of ways to create JS objects but I didn\'t know the Object.create(null)\'s one.

Question:

is it exactly the same as:

5条回答
  •  忘掉有多难
    2020-11-28 02:25

    They are not equivalent. {}.constructor.prototype == Object.prototype while Object.create(null) doesn't inherit from anything and thus has no properties at all.

    In other words: A javascript object inherits from Object by default, unless you explicitly create it with null as its prototype, like: Object.create(null).

    {} would instead be equivalent to Object.create(Object.prototype).


    In Chrome Devtool you can see that Object.create(null) has no __proto__ property, while {} does.

提交回复
热议问题