Royi Namir Royi Namir - 1 year ago 75
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


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

var p = {}


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download