Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of splats in object destructuring #941

Closed
lorefnon opened this Issue Nov 20, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@lorefnon
Copy link

lorefnon commented Nov 20, 2016

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 ?

@rhendric

This comment has been minimized.

Copy link
Collaborator

rhendric commented Nov 20, 2016

I've scratched my head at this one before; would like to hear other opinions but personally I'd be happy to call this a bug.

@summivox

This comment has been minimized.

Copy link
Contributor

summivox commented Nov 21, 2016

Good catch! I don't think there is any rationale for current behavior either.

Nitpick: the _objectWithoutProperties impl could have used an object instead of an array of strings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.