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)
's one.
Question:
is it exactly the same as:
var p = {}
vs
var p2 = Object.create(null);
?
javascript
add a comment |
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);
?
javascript
add a comment |
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);
?
javascript
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);
?
javascript
javascript
edited Jul 17 '18 at 22:58
tanguy_k
6,14322939
6,14322939
asked Mar 20 '13 at 8:12


Royi NamirRoyi Namir
74.8k98329588
74.8k98329588
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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.
add a comment |
They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.
var p = {};
Creates an object that inherits the properties and methods from
Object
.
var p2 = Object.create(null);
Creates an object that doesn't inherit anything.
If you are using an object as a map, and you create an object using method 1 above, then you have to be extra careful when doing lookups in the map. Because the properties and methods from Object
are inherited, your code may run into a case where there are keys in the map that you never inserted. For example, if you did a lookup on toString
, you would find a function, even though you never put that value there. You can work around that like this:
if (Object.prototype.hasOwnProperty.call(p, 'toString')) {
// we actually inserted a 'toString' key into p
}
Note that it is fine to assign something to p.toString
, it will simply override the inherited toString
function on p
.
Note that you can't just do p.hasOwnProperty('toString')
because you may have inserted a key "hasOwnProperty" into p
, so we force it to use the implementation in Object
.
On the other hand, if you use method 2 above, then you won't have to worry about things from Object
appearing in the map.
You can't check for the existence of a property with a simple if
like this:
// Unreliable:
if (p[someKey]) {
// ...
}
The value might be an empty string, might be false
, or null
, or undefined
, or 0
, or NaN
, etc. To check whether a property exists at all, you would still need to use Object.prototype.hasOwnProperty.call(p, someKey)
.
3
A simpler alternative for checking for the existence of a property is:if (someKey in p) {
– mrcrowl
Nov 4 '16 at 7:51
2
@mrcrowl Only if they usedObject.create(null)
. I prefer not to make assumptions like that, even if you were absolutely right that it usedObject.create(null)
, the code could change, the object could be replaced with one that inheritsObject
at some point.hasOwnProperty
always works.
– doug65536
Nov 4 '16 at 8:13
1
I feel being careful about something like this should be unnecessary. I appreciate your answer but documentation should give you the api necessary to work with whatever code you're working with. If you're grabbing some random code from github, then you can fork it and be safe from less documented updates. Not to mention{}
is so much more prevalent thanObject.create(null)
, that if your code accidentally grabs an inherited property at this point, you likely have way bigger bugs to worry about. I can only see people using Object.create(null) as a minor optimization.
– aaaaaa
Sep 13 '17 at 21:16
Double negation!!p[key]
works good withObject.create(null)
. ButhasKey = (key, input) => Object.prototype.hasOwnProperty.call(input, key)
is not bad either
– andreid
May 2 '18 at 10:03
> Note that you can't just do p.hasOwnProperty('toString') because you may have inserted a key "hasOwnProperty" into p, so we force it to use the implementation in Object. That's is unnecessary. In this case, you can't use any methods inp
because every method could be inserted and so become not safe.
– xianshenglu
Dec 6 '18 at 2:13
|
show 2 more comments
If someone is looking for implementing Object.create(null), just to know how it works. It is written using proto which is non-standard and hence, I do not recommend it.
function objectCreateMimic()
{
/*optional parameters: prototype_object, own_properties*/
var P = arguments.length>0?arguments[0]:-1;
var Q = arguments.length>1?arguments[1]:null;
var o = {};
if(P!==null && typeof P === "object")
{
o.__proto__ = P;
}
else if(P===null)
{
o.__proto__ = null;
}
if(Q!==null && typeof Q === "object")
{
for(var key in Q)
{
o[key] = Q[key];
}
}
return o;
}
Note: I wrote this, out of curiosity, and it is only written in simple terms, for instance, I am not transferring the property descriptors from the second object to the return object.
1
Note that as of ECMAScript's release in summer,__proto__
will now officially be part of the language.
– Chiru
Mar 20 '15 at 0:55
1
Why-1
inarguments.length>0?arguments[0]:-1;
?
– happy_marmoset
Dec 3 '15 at 9:39
@happy_marmoset late response, but it looks like it's just a non-null placeholder so that theObject
prototype is retained if the first argument isn't given. Variable names could be a lot better here.
– Mike Hill
Jul 31 '17 at 0:11
Also, the second parameter should describe the property descriptors rather than the actual properties themselves. See reference here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Mike Hill
Jul 31 '17 at 0:16
add a comment |
Creating objects by using {} will create an object whose prototype is Object.prototype which inherits the basic functions from Object prototype while creating objects by using Object.create(null) will create an empty object whose prototype is null.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15518328%2fcreating-js-object-with-object-createnull%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
edited Aug 28 '17 at 11:13
TryingToImprove
3,74011732
3,74011732
answered Mar 20 '13 at 8:37
Peter HerdenborgPeter Herdenborg
4,49011320
4,49011320
add a comment |
add a comment |
They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.
var p = {};
Creates an object that inherits the properties and methods from
Object
.
var p2 = Object.create(null);
Creates an object that doesn't inherit anything.
If you are using an object as a map, and you create an object using method 1 above, then you have to be extra careful when doing lookups in the map. Because the properties and methods from Object
are inherited, your code may run into a case where there are keys in the map that you never inserted. For example, if you did a lookup on toString
, you would find a function, even though you never put that value there. You can work around that like this:
if (Object.prototype.hasOwnProperty.call(p, 'toString')) {
// we actually inserted a 'toString' key into p
}
Note that it is fine to assign something to p.toString
, it will simply override the inherited toString
function on p
.
Note that you can't just do p.hasOwnProperty('toString')
because you may have inserted a key "hasOwnProperty" into p
, so we force it to use the implementation in Object
.
On the other hand, if you use method 2 above, then you won't have to worry about things from Object
appearing in the map.
You can't check for the existence of a property with a simple if
like this:
// Unreliable:
if (p[someKey]) {
// ...
}
The value might be an empty string, might be false
, or null
, or undefined
, or 0
, or NaN
, etc. To check whether a property exists at all, you would still need to use Object.prototype.hasOwnProperty.call(p, someKey)
.
3
A simpler alternative for checking for the existence of a property is:if (someKey in p) {
– mrcrowl
Nov 4 '16 at 7:51
2
@mrcrowl Only if they usedObject.create(null)
. I prefer not to make assumptions like that, even if you were absolutely right that it usedObject.create(null)
, the code could change, the object could be replaced with one that inheritsObject
at some point.hasOwnProperty
always works.
– doug65536
Nov 4 '16 at 8:13
1
I feel being careful about something like this should be unnecessary. I appreciate your answer but documentation should give you the api necessary to work with whatever code you're working with. If you're grabbing some random code from github, then you can fork it and be safe from less documented updates. Not to mention{}
is so much more prevalent thanObject.create(null)
, that if your code accidentally grabs an inherited property at this point, you likely have way bigger bugs to worry about. I can only see people using Object.create(null) as a minor optimization.
– aaaaaa
Sep 13 '17 at 21:16
Double negation!!p[key]
works good withObject.create(null)
. ButhasKey = (key, input) => Object.prototype.hasOwnProperty.call(input, key)
is not bad either
– andreid
May 2 '18 at 10:03
> Note that you can't just do p.hasOwnProperty('toString') because you may have inserted a key "hasOwnProperty" into p, so we force it to use the implementation in Object. That's is unnecessary. In this case, you can't use any methods inp
because every method could be inserted and so become not safe.
– xianshenglu
Dec 6 '18 at 2:13
|
show 2 more comments
They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.
var p = {};
Creates an object that inherits the properties and methods from
Object
.
var p2 = Object.create(null);
Creates an object that doesn't inherit anything.
If you are using an object as a map, and you create an object using method 1 above, then you have to be extra careful when doing lookups in the map. Because the properties and methods from Object
are inherited, your code may run into a case where there are keys in the map that you never inserted. For example, if you did a lookup on toString
, you would find a function, even though you never put that value there. You can work around that like this:
if (Object.prototype.hasOwnProperty.call(p, 'toString')) {
// we actually inserted a 'toString' key into p
}
Note that it is fine to assign something to p.toString
, it will simply override the inherited toString
function on p
.
Note that you can't just do p.hasOwnProperty('toString')
because you may have inserted a key "hasOwnProperty" into p
, so we force it to use the implementation in Object
.
On the other hand, if you use method 2 above, then you won't have to worry about things from Object
appearing in the map.
You can't check for the existence of a property with a simple if
like this:
// Unreliable:
if (p[someKey]) {
// ...
}
The value might be an empty string, might be false
, or null
, or undefined
, or 0
, or NaN
, etc. To check whether a property exists at all, you would still need to use Object.prototype.hasOwnProperty.call(p, someKey)
.
3
A simpler alternative for checking for the existence of a property is:if (someKey in p) {
– mrcrowl
Nov 4 '16 at 7:51
2
@mrcrowl Only if they usedObject.create(null)
. I prefer not to make assumptions like that, even if you were absolutely right that it usedObject.create(null)
, the code could change, the object could be replaced with one that inheritsObject
at some point.hasOwnProperty
always works.
– doug65536
Nov 4 '16 at 8:13
1
I feel being careful about something like this should be unnecessary. I appreciate your answer but documentation should give you the api necessary to work with whatever code you're working with. If you're grabbing some random code from github, then you can fork it and be safe from less documented updates. Not to mention{}
is so much more prevalent thanObject.create(null)
, that if your code accidentally grabs an inherited property at this point, you likely have way bigger bugs to worry about. I can only see people using Object.create(null) as a minor optimization.
– aaaaaa
Sep 13 '17 at 21:16
Double negation!!p[key]
works good withObject.create(null)
. ButhasKey = (key, input) => Object.prototype.hasOwnProperty.call(input, key)
is not bad either
– andreid
May 2 '18 at 10:03
> Note that you can't just do p.hasOwnProperty('toString') because you may have inserted a key "hasOwnProperty" into p, so we force it to use the implementation in Object. That's is unnecessary. In this case, you can't use any methods inp
because every method could be inserted and so become not safe.
– xianshenglu
Dec 6 '18 at 2:13
|
show 2 more comments
They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.
var p = {};
Creates an object that inherits the properties and methods from
Object
.
var p2 = Object.create(null);
Creates an object that doesn't inherit anything.
If you are using an object as a map, and you create an object using method 1 above, then you have to be extra careful when doing lookups in the map. Because the properties and methods from Object
are inherited, your code may run into a case where there are keys in the map that you never inserted. For example, if you did a lookup on toString
, you would find a function, even though you never put that value there. You can work around that like this:
if (Object.prototype.hasOwnProperty.call(p, 'toString')) {
// we actually inserted a 'toString' key into p
}
Note that it is fine to assign something to p.toString
, it will simply override the inherited toString
function on p
.
Note that you can't just do p.hasOwnProperty('toString')
because you may have inserted a key "hasOwnProperty" into p
, so we force it to use the implementation in Object
.
On the other hand, if you use method 2 above, then you won't have to worry about things from Object
appearing in the map.
You can't check for the existence of a property with a simple if
like this:
// Unreliable:
if (p[someKey]) {
// ...
}
The value might be an empty string, might be false
, or null
, or undefined
, or 0
, or NaN
, etc. To check whether a property exists at all, you would still need to use Object.prototype.hasOwnProperty.call(p, someKey)
.
They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.
var p = {};
Creates an object that inherits the properties and methods from
Object
.
var p2 = Object.create(null);
Creates an object that doesn't inherit anything.
If you are using an object as a map, and you create an object using method 1 above, then you have to be extra careful when doing lookups in the map. Because the properties and methods from Object
are inherited, your code may run into a case where there are keys in the map that you never inserted. For example, if you did a lookup on toString
, you would find a function, even though you never put that value there. You can work around that like this:
if (Object.prototype.hasOwnProperty.call(p, 'toString')) {
// we actually inserted a 'toString' key into p
}
Note that it is fine to assign something to p.toString
, it will simply override the inherited toString
function on p
.
Note that you can't just do p.hasOwnProperty('toString')
because you may have inserted a key "hasOwnProperty" into p
, so we force it to use the implementation in Object
.
On the other hand, if you use method 2 above, then you won't have to worry about things from Object
appearing in the map.
You can't check for the existence of a property with a simple if
like this:
// Unreliable:
if (p[someKey]) {
// ...
}
The value might be an empty string, might be false
, or null
, or undefined
, or 0
, or NaN
, etc. To check whether a property exists at all, you would still need to use Object.prototype.hasOwnProperty.call(p, someKey)
.
answered Jan 12 '14 at 19:29


doug65536doug65536
5,05413045
5,05413045
3
A simpler alternative for checking for the existence of a property is:if (someKey in p) {
– mrcrowl
Nov 4 '16 at 7:51
2
@mrcrowl Only if they usedObject.create(null)
. I prefer not to make assumptions like that, even if you were absolutely right that it usedObject.create(null)
, the code could change, the object could be replaced with one that inheritsObject
at some point.hasOwnProperty
always works.
– doug65536
Nov 4 '16 at 8:13
1
I feel being careful about something like this should be unnecessary. I appreciate your answer but documentation should give you the api necessary to work with whatever code you're working with. If you're grabbing some random code from github, then you can fork it and be safe from less documented updates. Not to mention{}
is so much more prevalent thanObject.create(null)
, that if your code accidentally grabs an inherited property at this point, you likely have way bigger bugs to worry about. I can only see people using Object.create(null) as a minor optimization.
– aaaaaa
Sep 13 '17 at 21:16
Double negation!!p[key]
works good withObject.create(null)
. ButhasKey = (key, input) => Object.prototype.hasOwnProperty.call(input, key)
is not bad either
– andreid
May 2 '18 at 10:03
> Note that you can't just do p.hasOwnProperty('toString') because you may have inserted a key "hasOwnProperty" into p, so we force it to use the implementation in Object. That's is unnecessary. In this case, you can't use any methods inp
because every method could be inserted and so become not safe.
– xianshenglu
Dec 6 '18 at 2:13
|
show 2 more comments
3
A simpler alternative for checking for the existence of a property is:if (someKey in p) {
– mrcrowl
Nov 4 '16 at 7:51
2
@mrcrowl Only if they usedObject.create(null)
. I prefer not to make assumptions like that, even if you were absolutely right that it usedObject.create(null)
, the code could change, the object could be replaced with one that inheritsObject
at some point.hasOwnProperty
always works.
– doug65536
Nov 4 '16 at 8:13
1
I feel being careful about something like this should be unnecessary. I appreciate your answer but documentation should give you the api necessary to work with whatever code you're working with. If you're grabbing some random code from github, then you can fork it and be safe from less documented updates. Not to mention{}
is so much more prevalent thanObject.create(null)
, that if your code accidentally grabs an inherited property at this point, you likely have way bigger bugs to worry about. I can only see people using Object.create(null) as a minor optimization.
– aaaaaa
Sep 13 '17 at 21:16
Double negation!!p[key]
works good withObject.create(null)
. ButhasKey = (key, input) => Object.prototype.hasOwnProperty.call(input, key)
is not bad either
– andreid
May 2 '18 at 10:03
> Note that you can't just do p.hasOwnProperty('toString') because you may have inserted a key "hasOwnProperty" into p, so we force it to use the implementation in Object. That's is unnecessary. In this case, you can't use any methods inp
because every method could be inserted and so become not safe.
– xianshenglu
Dec 6 '18 at 2:13
3
3
A simpler alternative for checking for the existence of a property is:
if (someKey in p) {
– mrcrowl
Nov 4 '16 at 7:51
A simpler alternative for checking for the existence of a property is:
if (someKey in p) {
– mrcrowl
Nov 4 '16 at 7:51
2
2
@mrcrowl Only if they used
Object.create(null)
. I prefer not to make assumptions like that, even if you were absolutely right that it used Object.create(null)
, the code could change, the object could be replaced with one that inherits Object
at some point. hasOwnProperty
always works.– doug65536
Nov 4 '16 at 8:13
@mrcrowl Only if they used
Object.create(null)
. I prefer not to make assumptions like that, even if you were absolutely right that it used Object.create(null)
, the code could change, the object could be replaced with one that inherits Object
at some point. hasOwnProperty
always works.– doug65536
Nov 4 '16 at 8:13
1
1
I feel being careful about something like this should be unnecessary. I appreciate your answer but documentation should give you the api necessary to work with whatever code you're working with. If you're grabbing some random code from github, then you can fork it and be safe from less documented updates. Not to mention
{}
is so much more prevalent than Object.create(null)
, that if your code accidentally grabs an inherited property at this point, you likely have way bigger bugs to worry about. I can only see people using Object.create(null) as a minor optimization.– aaaaaa
Sep 13 '17 at 21:16
I feel being careful about something like this should be unnecessary. I appreciate your answer but documentation should give you the api necessary to work with whatever code you're working with. If you're grabbing some random code from github, then you can fork it and be safe from less documented updates. Not to mention
{}
is so much more prevalent than Object.create(null)
, that if your code accidentally grabs an inherited property at this point, you likely have way bigger bugs to worry about. I can only see people using Object.create(null) as a minor optimization.– aaaaaa
Sep 13 '17 at 21:16
Double negation
!!p[key]
works good with Object.create(null)
. But hasKey = (key, input) => Object.prototype.hasOwnProperty.call(input, key)
is not bad either– andreid
May 2 '18 at 10:03
Double negation
!!p[key]
works good with Object.create(null)
. But hasKey = (key, input) => Object.prototype.hasOwnProperty.call(input, key)
is not bad either– andreid
May 2 '18 at 10:03
> Note that you can't just do p.hasOwnProperty('toString') because you may have inserted a key "hasOwnProperty" into p, so we force it to use the implementation in Object. That's is unnecessary. In this case, you can't use any methods in
p
because every method could be inserted and so become not safe.– xianshenglu
Dec 6 '18 at 2:13
> Note that you can't just do p.hasOwnProperty('toString') because you may have inserted a key "hasOwnProperty" into p, so we force it to use the implementation in Object. That's is unnecessary. In this case, you can't use any methods in
p
because every method could be inserted and so become not safe.– xianshenglu
Dec 6 '18 at 2:13
|
show 2 more comments
If someone is looking for implementing Object.create(null), just to know how it works. It is written using proto which is non-standard and hence, I do not recommend it.
function objectCreateMimic()
{
/*optional parameters: prototype_object, own_properties*/
var P = arguments.length>0?arguments[0]:-1;
var Q = arguments.length>1?arguments[1]:null;
var o = {};
if(P!==null && typeof P === "object")
{
o.__proto__ = P;
}
else if(P===null)
{
o.__proto__ = null;
}
if(Q!==null && typeof Q === "object")
{
for(var key in Q)
{
o[key] = Q[key];
}
}
return o;
}
Note: I wrote this, out of curiosity, and it is only written in simple terms, for instance, I am not transferring the property descriptors from the second object to the return object.
1
Note that as of ECMAScript's release in summer,__proto__
will now officially be part of the language.
– Chiru
Mar 20 '15 at 0:55
1
Why-1
inarguments.length>0?arguments[0]:-1;
?
– happy_marmoset
Dec 3 '15 at 9:39
@happy_marmoset late response, but it looks like it's just a non-null placeholder so that theObject
prototype is retained if the first argument isn't given. Variable names could be a lot better here.
– Mike Hill
Jul 31 '17 at 0:11
Also, the second parameter should describe the property descriptors rather than the actual properties themselves. See reference here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Mike Hill
Jul 31 '17 at 0:16
add a comment |
If someone is looking for implementing Object.create(null), just to know how it works. It is written using proto which is non-standard and hence, I do not recommend it.
function objectCreateMimic()
{
/*optional parameters: prototype_object, own_properties*/
var P = arguments.length>0?arguments[0]:-1;
var Q = arguments.length>1?arguments[1]:null;
var o = {};
if(P!==null && typeof P === "object")
{
o.__proto__ = P;
}
else if(P===null)
{
o.__proto__ = null;
}
if(Q!==null && typeof Q === "object")
{
for(var key in Q)
{
o[key] = Q[key];
}
}
return o;
}
Note: I wrote this, out of curiosity, and it is only written in simple terms, for instance, I am not transferring the property descriptors from the second object to the return object.
1
Note that as of ECMAScript's release in summer,__proto__
will now officially be part of the language.
– Chiru
Mar 20 '15 at 0:55
1
Why-1
inarguments.length>0?arguments[0]:-1;
?
– happy_marmoset
Dec 3 '15 at 9:39
@happy_marmoset late response, but it looks like it's just a non-null placeholder so that theObject
prototype is retained if the first argument isn't given. Variable names could be a lot better here.
– Mike Hill
Jul 31 '17 at 0:11
Also, the second parameter should describe the property descriptors rather than the actual properties themselves. See reference here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Mike Hill
Jul 31 '17 at 0:16
add a comment |
If someone is looking for implementing Object.create(null), just to know how it works. It is written using proto which is non-standard and hence, I do not recommend it.
function objectCreateMimic()
{
/*optional parameters: prototype_object, own_properties*/
var P = arguments.length>0?arguments[0]:-1;
var Q = arguments.length>1?arguments[1]:null;
var o = {};
if(P!==null && typeof P === "object")
{
o.__proto__ = P;
}
else if(P===null)
{
o.__proto__ = null;
}
if(Q!==null && typeof Q === "object")
{
for(var key in Q)
{
o[key] = Q[key];
}
}
return o;
}
Note: I wrote this, out of curiosity, and it is only written in simple terms, for instance, I am not transferring the property descriptors from the second object to the return object.
If someone is looking for implementing Object.create(null), just to know how it works. It is written using proto which is non-standard and hence, I do not recommend it.
function objectCreateMimic()
{
/*optional parameters: prototype_object, own_properties*/
var P = arguments.length>0?arguments[0]:-1;
var Q = arguments.length>1?arguments[1]:null;
var o = {};
if(P!==null && typeof P === "object")
{
o.__proto__ = P;
}
else if(P===null)
{
o.__proto__ = null;
}
if(Q!==null && typeof Q === "object")
{
for(var key in Q)
{
o[key] = Q[key];
}
}
return o;
}
Note: I wrote this, out of curiosity, and it is only written in simple terms, for instance, I am not transferring the property descriptors from the second object to the return object.
edited Nov 19 '14 at 7:07
answered Nov 19 '14 at 6:51
AravindAravind
2,50011631
2,50011631
1
Note that as of ECMAScript's release in summer,__proto__
will now officially be part of the language.
– Chiru
Mar 20 '15 at 0:55
1
Why-1
inarguments.length>0?arguments[0]:-1;
?
– happy_marmoset
Dec 3 '15 at 9:39
@happy_marmoset late response, but it looks like it's just a non-null placeholder so that theObject
prototype is retained if the first argument isn't given. Variable names could be a lot better here.
– Mike Hill
Jul 31 '17 at 0:11
Also, the second parameter should describe the property descriptors rather than the actual properties themselves. See reference here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Mike Hill
Jul 31 '17 at 0:16
add a comment |
1
Note that as of ECMAScript's release in summer,__proto__
will now officially be part of the language.
– Chiru
Mar 20 '15 at 0:55
1
Why-1
inarguments.length>0?arguments[0]:-1;
?
– happy_marmoset
Dec 3 '15 at 9:39
@happy_marmoset late response, but it looks like it's just a non-null placeholder so that theObject
prototype is retained if the first argument isn't given. Variable names could be a lot better here.
– Mike Hill
Jul 31 '17 at 0:11
Also, the second parameter should describe the property descriptors rather than the actual properties themselves. See reference here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Mike Hill
Jul 31 '17 at 0:16
1
1
Note that as of ECMAScript's release in summer,
__proto__
will now officially be part of the language.– Chiru
Mar 20 '15 at 0:55
Note that as of ECMAScript's release in summer,
__proto__
will now officially be part of the language.– Chiru
Mar 20 '15 at 0:55
1
1
Why
-1
in arguments.length>0?arguments[0]:-1;
?– happy_marmoset
Dec 3 '15 at 9:39
Why
-1
in arguments.length>0?arguments[0]:-1;
?– happy_marmoset
Dec 3 '15 at 9:39
@happy_marmoset late response, but it looks like it's just a non-null placeholder so that the
Object
prototype is retained if the first argument isn't given. Variable names could be a lot better here.– Mike Hill
Jul 31 '17 at 0:11
@happy_marmoset late response, but it looks like it's just a non-null placeholder so that the
Object
prototype is retained if the first argument isn't given. Variable names could be a lot better here.– Mike Hill
Jul 31 '17 at 0:11
Also, the second parameter should describe the property descriptors rather than the actual properties themselves. See reference here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Mike Hill
Jul 31 '17 at 0:16
Also, the second parameter should describe the property descriptors rather than the actual properties themselves. See reference here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Mike Hill
Jul 31 '17 at 0:16
add a comment |
Creating objects by using {} will create an object whose prototype is Object.prototype which inherits the basic functions from Object prototype while creating objects by using Object.create(null) will create an empty object whose prototype is null.
add a comment |
Creating objects by using {} will create an object whose prototype is Object.prototype which inherits the basic functions from Object prototype while creating objects by using Object.create(null) will create an empty object whose prototype is null.
add a comment |
Creating objects by using {} will create an object whose prototype is Object.prototype which inherits the basic functions from Object prototype while creating objects by using Object.create(null) will create an empty object whose prototype is null.
Creating objects by using {} will create an object whose prototype is Object.prototype which inherits the basic functions from Object prototype while creating objects by using Object.create(null) will create an empty object whose prototype is null.
answered Apr 12 '18 at 19:16


Praveen KishorPraveen Kishor
6951716
6951716
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15518328%2fcreating-js-object-with-object-createnull%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown