I'm trying to spread the Javascript Error object(Standard built-in objects). I'm getting the empty object as output.
let error = new Error('error');
console.log({...error});
output:
{}
What is the reason for not spreading the Error object?
I'm trying to spread the Javascript Error object(Standard built-in objects). I'm getting the empty object as output.
let error = new Error('error');
console.log({...error});
output:
{}
What is the reason for not spreading the Error object?
This is because the spread syntax in object literals "copies own enumerable properties from a provided object onto a new object".
None of the own properties of your Error object are enumerable.
var error = new Error('error');
var props = Object.getOwnPropertyDescriptors(error);
console.log(props); // none of these are enumerable
So the spread syntax copies nothing. If it had an enumerable value, then it would have copied it:
var error = new Error('error');
error.foo = 'bar';
console.log({...error});