r/javascript • • Apr 28 '19

7 Useful JavaScript Tricks

https://davidwalsh.name/javascript-tricks
505 Upvotes

36 comments sorted by

View all comments

9

u/Owmelicious Apr 29 '19

I'm fairly new to Javascript. What is a good practical example where one needs a pure "dictionary" object?

5

u/brown59fifty Apr 29 '19 edited Apr 29 '19

Great question! It's hard to give an example, because it's a really very rare case - most when you want to treat your variable more like a kind of associative array than an Object instance (what can be welcome when someone came to JS world from some static typed language). Because you inherit from null, then you don't have access to methods on Object.prototype (like constructor, hasOwnProperty or valueOf) which results like this:

const dict = Object.create(null);
dict instanceof Object // false
dict.toString() // Uncaught TypeError: dict.toString is not a function

This way of creating things can be used when you want to build some API library, which couldn't be extended from outside (eg. by passing values to Object.prototype). But in most cases you don't have to care.