Creating JS object with Object.create(null)?












111















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);


?










share|improve this question





























    111















    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);


    ?










    share|improve this question



























      111












      111








      111


      31






      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);


      ?










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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
























          4 Answers
          4






          active

          oldest

          votes


















          151














          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






          share|improve this answer

































            84














            They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.





            1. var p = {};



              Creates an object that inherits the properties and methods from Object.




            2. 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).






            share|improve this answer



















            • 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 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





              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











            • > 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





















            0














            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.






            share|improve this answer





















            • 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 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











            • 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





















            0














            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.






            share|improve this answer























              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              151














              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






              share|improve this answer






























                151














                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






                share|improve this answer




























                  151












                  151








                  151







                  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






                  share|improve this answer















                  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







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  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

























                      84














                      They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.





                      1. var p = {};



                        Creates an object that inherits the properties and methods from Object.




                      2. 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).






                      share|improve this answer



















                      • 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 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





                        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











                      • > 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


















                      84














                      They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.





                      1. var p = {};



                        Creates an object that inherits the properties and methods from Object.




                      2. 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).






                      share|improve this answer



















                      • 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 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





                        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











                      • > 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
















                      84












                      84








                      84







                      They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.





                      1. var p = {};



                        Creates an object that inherits the properties and methods from Object.




                      2. 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).






                      share|improve this answer













                      They are definitely not equivalent. I'm writing this answer to more fully explain why it makes a difference.





                      1. var p = {};



                        Creates an object that inherits the properties and methods from Object.




                      2. 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).







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      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 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





                        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











                      • > 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
















                      • 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 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





                        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











                      • > 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










                      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













                      0














                      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.






                      share|improve this answer





















                      • 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 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











                      • 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


















                      0














                      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.






                      share|improve this answer





















                      • 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 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











                      • 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
















                      0












                      0








                      0







                      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.






                      share|improve this answer















                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      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 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











                      • 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





                        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 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











                      • 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













                      0














                      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.






                      share|improve this answer




























                        0














                        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.






                        share|improve this answer


























                          0












                          0








                          0







                          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.






                          share|improve this answer













                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 12 '18 at 19:16









                          Praveen KishorPraveen Kishor

                          6951716




                          6951716






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                              Npm cannot find a required file even through it is in the searched directory