Royi Namir Royi Namir - 25 days ago 7
Javascript Question

Creating Js object with Object.create(null)?

I know a lot of ways to create JS objects but I didn't know the

Object.create(null)
one.

and so... is it exactly the same as:

var p = {}


vs

var p2 = Object.create(null);


?

Answer Source

I think they are not equivalent. If I'm not mistaking, {}.constructor.prototype == Object.prototype while Object.create(null) doesn't inherit from anywhere and thus has no properties at all.

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

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


In Chrome Devtool you can see that Object.create(null) have no __proto which {} have.

enter image description here