object-destructuring

Object spread an Error results in no message property [duplicate]

为君一笑 提交于 2020-06-23 12:18:57
问题 This question already has an answer here : Why can't I see the keys of an Error object? (1 answer) Closed 5 months ago . I'm trying to spread an Error so that I can alter the error without affecting the original error. const error = new Error('Error test'); const freeError = {...error}; console.log(error, freeError); But the output is an empty object {} . I'm expecting the freeError have at least a message property, but there is none . Is this a part of JavaScript feature or is there

How to destructure an object based on a dynamic defined variable

◇◆丶佛笑我妖孽 提交于 2020-02-02 06:50:11
问题 I am working with an immutable object that I need to add subtract an array of values from. I know it's possible using ES6 destructing with the following. const {countries, remainder} = someObj // {countries:...,languages:..., ...keys}; This will end me up with the remainder being the new Object without the countries key. Therefore I figured out that I could use reduce on an array to go through and remove all the values from the object, returning a new object in the end, using the default

Escape reserved keywords in object destructing assignment

左心房为你撑大大i 提交于 2019-12-13 03:24:17
问题 Is it possible to use reserved keywords in an object destructing assignment. Specifically I am trying to handle JSON with property property named default. //Doesn't compile class FooBar { constructor({foo, default}) { this.foo = foo; this.default = default; } } /* json from server {foo: "bar", default: true} */ new FooBar(json); 回答1: It's possible to use them as a property name, but not as a variable name. Choose a different target: class FooBar { constructor({foo, default: def}) { this.foo =

Why is this JavaScript not interpreted as a code block when semi-colon is used?

吃可爱长大的小学妹 提交于 2019-12-07 03:42:11
问题 In Chrome version ^72 if I run the following JavaScript there are no errors. { prop: p } = { prop: 'prop' } >> { prop: 'prop' } So the line of code is interpreted as an expression statement, unexpectedly. But if I run the same code with a semi-colon at the end it runs as expected. { prop: p } = { prop: 'prop' }; >> Uncaught SyntaxError: Unexpected token = This is expected since the initial { tells the JavaScript engine that it is a code block unless we disambiguate with parentheses. Why does

Why is this JavaScript not interpreted as a code block when semi-colon is used?

旧时模样 提交于 2019-12-05 08:22:05
In Chrome version ^72 if I run the following JavaScript there are no errors. { prop: p } = { prop: 'prop' } >> { prop: 'prop' } So the line of code is interpreted as an expression statement, unexpectedly. But if I run the same code with a semi-colon at the end it runs as expected. { prop: p } = { prop: 'prop' }; >> Uncaught SyntaxError: Unexpected token = This is expected since the initial { tells the JavaScript engine that it is a code block unless we disambiguate with parentheses. Why does this occur with the semi-colon but not without it? Why does this occur with the semi-colon but not

Destructure object parameter, but also have reference to the parameter as an object? [duplicate]

送分小仙女□ 提交于 2019-12-01 02:22:09
问题 This question already has answers here : ES6 destructuring function parameter - naming root object (3 answers) Closed last year . With ES6 you can destructure objects in function arguments: ({name, value}) => { console.log(name, value) } The equivalent ES5 would be: function(params) { console.log(params.name, params.value) } But what if I would like a reference both to the params object and the nested properties value and name ? This is the closest I got, but the drawback is that it won't