Nailing object property order

Scott Sauyet scott at sauyet.com
Thu Apr 16 14:16:50 UTC 2015


I don't have pointers to other es-discuss threads where this was
mentioned, but I do have a reason of my own for wanting this.

I started and help maintain Ramda [1], a functional programming
library for ES.  It is a collection of utility functions similar in
scope to Underscore or lodash, although with a different underlying
philosophy and a somewhat different API design.  One of the features
most requested is one that we've had requested over and over [2]: a
`foldObj` function that works like a normal `fold`/`reduce` on a list,
but works instead on the properties of an object.

There are a few tricky questions about such a function, including what
to do with properties of the prototypes, but the only sticking point
is one of iteration order.  Because this is underspecified, and
because we want our library to be dependent on specified behavior and
not simply _de facto_ implementation decisions, we have resisted this
popular feature request.  At the moment there is no way to know if,
say

    reduce(concat, '"", keys({start: 0, end: 10}))

will yield `startend` or `endstart`.  Granted, in most (all?) modern
implementations it will be the former, but we can't count on it.

This will not address a related issue I often discuss alongside this,
which is that the lodash-style implementation has this somewhat
surprising behavior:

    var diff = (a, b) => a - b,
        obj1 = {a: 1, b: 2, c: 3};
        obj2 = {b: 2, a: 1, c: 3};
    _.isEqual(obj1, obj2); //=> true
    _.reduce(obj1, diff); //=> 2
    _.reduce(obj2, diff); //=> 4

But if we had a specified key-iteration order, I would be more than
willing to live with and document such an oddity, if I could point to
the specification to show exactly _how_ the objects differ.  Heck, if
necessary, I could even write an `isStrictlyEqual` that goes beyond
property equality and checks key order.

So, I wasn't part of the decision-making, but I do endorse this change.

Do you see good reasons *not* to do it, or are you just wondering if
there actually were pros to this?

  -- Scott

  [1] https://github.com/ramda/ramda
  [2] https://github.com/ramda/ramda/issues/257,
      https://github.com/ramda/ramda/issues/364,
      https://github.com/ramda/ramda/issues/546,
      https://github.com/ramda/ramda/issues/625
      https://github.com/ramda/ramda/issues/656,
      and certainly others...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150416/4b4a5a00/attachment.html>


More information about the es-discuss mailing list