Skip to content

Use of splats in object destructuring #941

Description

@lorefnon

Current behavior when using splats while destructuring objects is non-intuitive (if not buggy):

x = ({name, age, ...rest}) -> 
  rest

Results in:

var x;
x = function(arg$){
  var name, age, rest;
  name = arg$.name, age = arg$.age, rest = import$({}, arg$.rest);
  return rest;
};
function import$(obj, src){
  var own = {}.hasOwnProperty;
  for (var key in src) if (own.call(src, key)) obj[key] = src[key];
  return obj;
}

People coming from ES6 are likely to expect rest to include attributes other than name and age.

Analogous example using babel:

var x = ({a, b, ...rest}) =>
  console.log(rest)

compiles to:

"use strict";

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var x = function x(_ref) {
  var a = _ref.a,
      b = _ref.b,
      rest = _objectWithoutProperties(_ref, ["a", "b"]);

  return console.log(rest);
};

Is there a rationale behind the current behavior ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions