What is the reason for not spread (es 6 spread operator) javascript Error object [duplicate]

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?

Answers:

Answer

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});

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.