destructuring

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

Import ES6 module elements directly or destructure const assignment following the import? [closed]

ⅰ亾dé卋堺 提交于 2019-12-06 09:22:37
Closed . This question is opinion-based . It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post . Closed 3 years ago . Somewhat new to ES6 modules and find myself torn between the following 2 patterns... Pattern #1 - Destructuring on import import { func1, func2, func3 } from 'my-funcs'; Pattern #2 - Destructuring on const import * as myFuncs from 'my-funcs'; const { func1, func2, func3 } = myFuncs; I like the pattern #1 for its brevity, but I also like pattern #2 as it seems more explicit.

Complex data manipulation in Clojure

不羁的心 提交于 2019-12-06 05:47:02
问题 I'm working on a personal market analysis project. I've got a data structure representing all the recent turning points in the market, that looks like this: [{:high 1.121455, :time "2016-08-03T05:15:00.000000Z"} {:low 1.12109, :time "2016-08-03T05:15:00.000000Z"} {:high 1.12173, :time "2016-08-03T04:30:00.000000Z"} {:high 1.121925, :time "2016-08-03T00:00:00.000000Z"} {:high 1.12215, :time "2016-08-02T23:00:00.000000Z"} {:high 1.12273, :time "2016-08-02T21:15:00.000000Z"} {:high 1.12338,

Renaming remaining properties variable when object destructuring in TypeScript

廉价感情. 提交于 2019-12-05 22:53:17
EDIT: I opened an issue related to this on github: https://github.com/Microsoft/TypeScript/issues/21265 It seems that { ...other: xother } is not valid JS, neither TS, code and it should not even compile. Original question: Let's assume the following example: const values = { a: "1", b: "2", c: "3", d: "4" }; const { a: va, b: vb, ...other } = values; where a new variable name va is assigned for property a . Is it valid, according to TypeScript specs, to do the same with the remaining properties ...other ? Something like: const { a: va, b: vb, ...other: vother } = values; I know it works, I've

Nested Object destructuring [duplicate]

百般思念 提交于 2019-12-05 18:17:44
This question already has an answer here: ES6 Object Destructuring Default Parameters 1 answer When destructuring objects, I sometimes run into the issue of not knowing whether or not keys exist, and then trying to pull values from them. This obviously errors, since they are undefined. For example: Expecting something like this: { user: { name: { first: 'Trey', last: 'Hakanson' } } } But I actually get this: { user: {} } and attempting to destructure like this errors: const { user: { name: { first: firstName, last: lastName } } } = data is there any way to assign a default value earlier in the

How to destructure an object's property and property's properties. ES6 Javascript

主宰稳场 提交于 2019-12-05 13:39:42
I have an object const complicatedObject = { propertyA: { property1: 1, property2: 2 }, propertyB: { property1: 1, property2: 2 } } If I want to grab propertyA I do const { propertyA } = complicatedObject console.log(propertyA) // { property1: 1, property2: 2} If I want to grab propertyA's property1 value I do const { propertyA: { property1 } } = complicatedObject console.log(property1) // 1 I can grab propertyA and propertyA's property1 this way. const { propertyA, propertyA: { property1 } } = complicatedObject console.log(propertyA) // { property1: 1, property2: 2} console.log(property1) //

React-native animated.event custom onScroll listener

好久不见. 提交于 2019-12-05 10:55:17
In official react-native documentation there is a section about Animated.event method. As example they use following code: onScroll={Animated.event( // scrollX = e.nativeEvent.contentOffset.x [{ nativeEvent: { contentOffset: { x: scrollX } } }] )} I would like to map correct values to Animated.event method and I would also like to map onScroll callback parameters to my own callback. Basically I would like to do something like this: onScroll={(event) => { myOwnCallback(event.nativeEvent.contentOffset.x) Animated.event( // scrollX = e.nativeEvent.contentOffset.x [{nativeEvent: { contentOffset: {

Destructure array to object property keys

北城余情 提交于 2019-12-05 10:40:22
问题 I have an array of values like: const arr = [1,2,3]; Is there any way I can use destructuring to create the following output? If not, what is the easiest way I can do this in ES6 (or later)? const obj = { one: 1, two: 2, three: 3 }; I tried this, but I guess it doesn't work as this is the syntax for computed keys: const arr = [1,2,3]; const obj = { [one, two, three] = arr }; 回答1: I don't believe there's any structuring/destructuring solution to doing that in a single step, no. I wanted

Combining the Parameter Properties shorthand with Destructuring in TypeScript

百般思念 提交于 2019-12-05 09:38:03
问题 EDIT I logged an issue on TypeScript's Github repo and they're accepting PRs for implementing it. In TypeScript, when we want to automatically create properties in our class from the constructor definition, we can take advantage of the Parameter Properties shorthand, e.g: class Person { constructor(public firstName : string, public lastName : number, public age : number) { } } And then, the transpiled Javascript will be: var Person = (function () { function Person(firstName, lastName, age) {

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