Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
106
rated 0 times [  111] [ 5]  / answers: 1 / hits: 41351  / 11 Years ago, wed, march 20, 2013, 12:00:00

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:


var p = {}

vs


var p2 = Object.create(null);

?


More From » javascript

 Answers
0

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.



enter


[#79471] Tuesday, March 19, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
patienceannel

Total Points: 674
Total Questions: 101
Total Answers: 101

Location: Northern Mariana Islands
Member since Fri, Jan 15, 2021
3 Years ago
patienceannel questions
Fri, Mar 11, 22, 00:00, 2 Years ago
Tue, Oct 20, 20, 00:00, 4 Years ago
Wed, Jul 24, 19, 00:00, 5 Years ago
;