Skip to content

Will adding user properties to arrays break existing code, like recursive serialization/deserialization or object transformation? #2

@justingrant

Description

@justingrant

If this propoosal is added to the language, it'd make it more ergonomic to add properties to arrays. But adding properties to arrays will likely break a lot of existing code that assumes that arrays are mutually exclusive from plain objects. So I'm not sure that we want to make it more ergonomic!

For example, JS code for object serialization/deserialization or recursive transformation of objects typically assumes that arrays don't have userland-added properties. Here's just one example that I pulled from one of my projects:

/** Recursively sort keys in an object */
function sortKeys(obj) {
  if (Array.isArray(obj)) {
    return obj.map(sortKeys);
  } else if (obj && typeof obj === 'object') {
    return Object.keys(obj)
      .sort()
      .reduce((sorted, key) => {
        sorted[key] = sortKeys(obj[key]);
        return sorted;
      }, {});
  }
  return obj;
}

A more comprehensive example is here, from MongoDB's BSON library that is responsible for serializing data to/from MongoDB.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions