Let's say I have this code:
const {x, y} = point;
Babel will turn this into:
var _point = point, x = _point.x, y = _point.y;
Which is good, but what if the point is not defined? Now I get the error message:
"Cannot read property 'x' of undefined" .
So how do I avoid this?
I want to do something like
const {x, y} = {} = point;
but this is a syntax error.
I can only see that this is an option:
const {x, y} = point || {};
Which dummy goes to:
var _ref = point || {}, x = _ref.x, y = _ref.y;
Here we create an object to avoid an undefined error. It seems wasteful.
Is there any syntax that I am missing to avoid this? Something that could go into something like this:
var x, y; if (typeof point !== 'undefined') { x = point.x; y = point.y; }
source share