Skip to content

Intelligent destructuring

Would be neat if this...

function rect ({ x, y, width, height, color }) {
  ctx.fillStyle = color;
  ctx.fillRect( x, y, width, height );
}

...compiled to this...

function rect ( ref ) {
  ctx.fillStyle = ref.color;
  ctx.fillRect( ref.x, ref.y, ref.width, ref.height );
}

...instead of this:

function rect (ref) {
  var x = ref.x;
  var y = ref.y;
  var width = ref.width;
  var height = ref.height;
  var color = ref.color;

  ctx.fillStyle = color;
  ctx.fillRect( x, y, width, height );
}

Should be pretty easy to determine which form to go with based on how many times a reference is used.