destructuring

Can I use destructuring somehow inside an if statement?

别说谁变了你拦得住时间么 提交于 2019-12-10 00:03:26
问题 is there a way I could do something like this ? if({color, size, shape} = this.props){ console.log('Received all props!'); } else { console.log('There are some missing props'); } I want to know if I received all needed data through my Component's props, and if not then throw an error. It's for creating reusable components. 回答1: You could use default values: function missing(prop) { throw new TypeError(`there is a missing ${prop} property`); } … try { const { color = missing("color"), size =

let forms : How to access destructured symbols in a macro?

强颜欢笑 提交于 2019-12-08 21:21:22
问题 I'm trying to write a macro which expands to a let form with destructuring. My problem is that I would like to have the list of the symbols that are defined in the let form, including those obtained by destruturing. Use case I'm trying to factor out this kind of behavior, for validation for example : (let [a (foo bar) {x :x, y :y, {u :u, v: v :as nested-map} :nested} some-map] (and x y nested-map u v ; testing truthiness (valid-a? a) (valid-x? x) (valid-y? y) (valid-nested? nested-map) (valid

ES6 Destructuring assignment with `this`

こ雲淡風輕ζ 提交于 2019-12-08 17:00:22
问题 The code below works. Is there a way that is more convenient, if possible even a one-liner? const { nextUrl, posts } = await postService.getCommunityPosts(6); this.communityPosts = posts; this.nextUrl = nextUrl; I know about giving destructured properties aliases but I don't think that helps in this case. MDN doesn't say anything about that case. 回答1: You can assign to the properties of an existing object by giving aliases and encapsulating the assignment in parentheses (await codepen). const

Array destructuring assignment not working in v8 with harmony option in Node.js

筅森魡賤 提交于 2019-12-08 01:04:34
问题 I want to learn how to enable harmony v8 options in Node, and my node version is: $ node -v v5.5.0 Use ES6 destructuring as an example for testing $ cat destructure.js 'use strict' var a, b [a, b] = [1, 2] console.log(a, b) Run it straight gets error as expected. $ node destructure.js /usr/home/mko_io/pure-js-files/destructure.js:3 [a, b] = [1, 2] ^^^^^^ But get the same error, after the flag has been set: $ node --harmony_destructuring destructure.js /usr/home/mko_io/pure-js-files

What does this array-style destructuring on a function do in ES6?

独自空忆成欢 提交于 2019-12-08 00:55:29
问题 I read through the redux-actions tutorial, and am confused by their use of (what I believe to be) destructuring. Below is an example ( increment & decrement are both functions returned by the createAction function). const { createAction, handleActions } = window.ReduxActions; const reducer = handleActions( { [increment]: state => ({ ...state, counter: state.counter + 1 }), [decrement]: state => ({ ...state, counter: state.counter - 1 }) }, defaultState ); Here's another example of this being

Renaming remaining properties variable when object destructuring in TypeScript

别说谁变了你拦得住时间么 提交于 2019-12-07 17:38:57
问题 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

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

蹲街弑〆低调 提交于 2019-12-07 11:21:23
问题 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 } }

Nested Object destructuring [duplicate]

天大地大妈咪最大 提交于 2019-12-07 09:00:42
问题 This question already has an answer here : ES6 Object Destructuring Default Parameters (1 answer) Closed 2 years ago . 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:

Can destructuring assignment be used to effect a projection in CoffeeScript?

本秂侑毒 提交于 2019-12-07 07:03:00
问题 I'm having some trouble understanding destructuring assignment in CoffeeScript. The documentation contains a couple of examples which together seem to imply that renaming objects during assignment can be used to project (i.e. map, translate, transform) a source object. I am trying to project a = [ { Id: 1, Name: 'Foo' }, { Id: 2, Name: 'Bar' } ] into b = [ { x: 1 }, { x: 2 } ] . I've tried the following without success; I've clearly misunderstood something. Can anyone explain whether this is

React-native animated.event custom onScroll listener

妖精的绣舞 提交于 2019-12-07 05:42:22
问题 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