Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 292
mr93 Avatar asked Nov 06 '18 06:11

mr93


People also ask

How to use the spread operator in ES6?

ES6 has added spread property to object literals in javascript. The spread operator (…) with objects is used to create copies of existing objects with new or updated values or to make a copy of an object with more properties. Let’s take at an example of how to use the spread operator on an object, const user1 = {

What is the spread operator in JavaScript?

When … occurs in function call or alike,its called a spread operator. Spread operator can be used in many cases,like when we want to expand,copy,concat,with math object.

What is spread property in ES6 JavaScript?

ES6 has added spread property to object literals in javascript. The spread operator ( …) with objects is used to create copies of existing objects with new or updated values or to make a copy of an object with more properties.

What is the difference between rest and spread in JavaScript?

The main difference between rest and spread is that the rest operator puts the rest of some specific user-supplied values into a JavaScript array. But the spread syntax expands iterables into individual elements.


1 Answers

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});
like image 160
Kaiido Avatar answered Nov 09 '22 08:11

Kaiido