Deconstructed object return
I was working on a project that I needed a few keys from an object. With JavaScript I like to see if it's possible to make only one line.
const position = element => (({x, y}) => ({x, y}))(element);
// {x: 100, y: 200}
return position({x: 100, y: 200, height: 100, width: 200});
The code that is returned is an Immediately Invoked Function Expression (IIFE), so the element that is passed in to position()
will be deconstructed to get x
and y
out of the object. Then the function returns x
and y
as an object.