Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Dictionary Objects in JavaScript and TypeScript (2017) (jessitron.com)
35 points by luu on Oct 21, 2018 | hide | past | favorite | 11 comments


JavaScript has a map type that accepts non-string keys.


The syntax for instantiating Maps is really ham handed. An array of arrays of key value pairs. I get it. I just wish there was a better syntax. We need Map literals and for that I think we need another type of brace character.

I just want Python's dict everywhere. :)


If you're ok with string keys, you can always do `new Map(Object.entries(obj))`. Alternatively, just implement the `iterable` interface on your custom data type.

Literals are nice sugar, but it hardly seems "ham handed".


Everything is sugar. The right sweetness makes the language.


Or they could have just made

  {[myObject]: myValue}
"just work" as opposed to doing this crap:

  {"[object Object]": myValue}
Lua tables are really simple and let you use references as keys.


Eh, special-casing specific syntax over regular objects would be a nightmare.


Alas, the ES6 Map/Set implementation almost only does identity-equality (===) so you have to jump through extra hoops in order to get something like Python or Java. (That is, with equals() / hashCode() or __eq__() / __hash__() respectively.)

In other words, you can't have two (immutable) "Address" objects resolve to the same storage spot, even if they are functionally identical in every way.

I had a mini-project a while back, making a Typescript 3 Map/Set class which would be familiar to Java/Python folks. The main difference being no predefined functions, you simply pass the data-structure some functions it can use to operate on the objects.

I'd like to throw it up on Github, but unfortunately it's still technically the property of my employer. Think it's worth asking?


I've always thought this was very interesting; specifically, the ability to use objects and functions as keys. [1]

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...


Is there a use case for that? I've never run into one.


As one potential example, it would be useful for implementing something similar to Racket's 'dispatch-rules' (https://docs.racket-lang.org/web-server/dispatch.html#%28for...) in JavaScript.

Specifically, the second return value from this macro is a procedure that generates URLs given a handler function. This allows one to go both ways in URL-handler mapping (generating URLs from handlers/controllers) instead of just one-way (mapping requests to specific handlers/controllers) as in a conventional router in Rails or Django.

Once you learn about this pattern, you start to see it in more places. A slightly different example, I am working on a mail router (something akin to procmail) in Racket, and have used a similar pattern to map from predicates (procedures taking an email and returning true or false depending on certain conditions) to symbols representing those predicates in a DSL. A hash table with procedures for keys lets me do this mapping in both directions should I need to.

(Note that I am not actually sure whether dispatch-rules itself uses procedure-keyed dictionaries or hash tables under the hood.)


Map is really handy for writing memoization-type functions:

  const memoize = func => {
    const cache = new Map();

    const memoized = arg => {
      const cached = cache.get(arg);

      if (cached !== undefined) {
        return cached;
      }

      const result = func(arg);
      cache.set(arg, result);

      return result;
    };

    memoized.clearCache = cache.clear.bind(cache);

    return memoized;
  };
(Also, you could use WeakMap instead of Map if you knew the memoized function would only ever receive objects as arguments.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: