I have seen Object.create(null) being used in numerous tutorials (e.g.
http://killdream.github.com/blog/2011/10/understanding-javascript-oop/),
but I don't understand what's the point of it. Are there situations where
it would be preferable to not inherit from Object.prototype?

If you want a hash map it makes things easier when your map does not inherit from Object.prototype

var hashMap = {};
"toString" in hashMap; // true

var hashMap = Object.create(null);
"toString" in hashMap; // false

Of course, you could use hashMap.hasOwnProperty() in the first example (but not in the second!), it's just more convenient to use "in".

Another use case is when (god forbid) someone extended Object.prototype. With Object.create(null) you now have an object that does not inherit the (enumerable) extensions.

Matt


--
Follow me on twitter.com/gweax

--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to