Lets say I have this code:
const {x, y} = point;
Babel will turn this into:
var _point = point,
x = _point.x,
y = _poin
To handle undefined error in ES6 object destructuring, you can do something like following
const {x, y} = {...point};
console.log(x) // undefined
console.log(y) // undefined
[…] what if point is undefined? Now I get an error: "Cannot read property 'x' of undefined"
So how do I avoid this?
If you want to write clear code, you can explicitly check that condition:
let { x, y };
if (typeof point === 'undefined') {
x = y = undefined;
} else {
{ x, y } = point;
}