Fwd: proposal of function return with object destructor

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: proposal of function return with object destructor

Новиков Денис
Hi! Sorry, I don't sure that it is a place for feature request - pls, write if I wrong.
My proposal is to allow return of part of object fields.
 
Yesterday I wrote some ugly code:
const { id, displayName, type, isEnabled } = segment;
  return { id, displayName, type, isEnabled };
 
There is problem with duplication. I think next variant looks much better:
 
return { id, displayName, type, isEnabled } = segment;
 
 
Thank you and sorry if this email is incorrect for proposals.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: proposal of function return with object destructor

Isiah Meadows-2

You did start in the right spot! Search esdiscuss.com for "pick notation", though - it's pretty much the same thing, but an expression instead.

(TL;DR: it was rejected as overly specific, since it's maybe saving a couple lines of code, and it's also pretty easy to write a utility function if you find yourself doing this a lot. Also, Lodash and similar do have utility methods for this kind of thing.)


On Tue, Oct 10, 2017, 07:15 Новиков Денис <[hidden email]> wrote:
Hi! Sorry, I don't sure that it is a place for feature request - pls, write if I wrong.
My proposal is to allow return of part of object fields.
 
Yesterday I wrote some ugly code:
const { id, displayName, type, isEnabled } = segment;
  return { id, displayName, type, isEnabled };
 
There is problem with duplication. I think next variant looks much better:
 
return { id, displayName, type, isEnabled } = segment;
 
 
Thank you and sorry if this email is incorrect for proposals.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: proposal of function return with object destructor

Michael Rosefield
In reply to this post by Новиков Денис
I've had similar instances of using deconstruction as an annoying man-in-the-middle step; perhaps what is needed is a deconstruction operator, since assignment wouldn't work in, say a concise arrow function because it's not an expression:


```
// works, but is verbose (repeats declaration of deconstructed variables)
const result = doSomething()
  .then({ propA, foo: { propB }) => ({ propA, propB }))
  .then(doSomethingElse);

// doesn't work as assignment; assignment is not expression
const result = doSomething()
  .then(x => { { propA, foo: { propB } } = x)
  .then(doSomethingElse);
```
Perhaps what is needed is an operator that is effectively shorthand for creating a temporary object to hold the deconstructed assignments, and is allowed in expressions:

```
{ propA, foo: { propB } }) =< x
// equivalent to
Object.assign({}, { propA: x.propA }, { propB: x.foo.propB });

const result = doSomething()
  .then(x => { { propA, foo: { propB } } <= x)
  .then(doSomethingElse);
```

Ideally, I'd--

and then this email comes into the thread:

Isiah Meadows
> You did start in the right spot! Search esdiscuss.com for "pick notation", though - it's pretty much the same thing, but an expression instead.

Well damnit...

On Tue, 10 Oct 2017 at 12:15 Новиков Денис <[hidden email]> wrote:
Hi! Sorry, I don't sure that it is a place for feature request - pls, write if I wrong.
My proposal is to allow return of part of object fields.
 
Yesterday I wrote some ugly code:
const { id, displayName, type, isEnabled } = segment;
  return { id, displayName, type, isEnabled };
 
There is problem with duplication. I think next variant looks much better:
 
return { id, displayName, type, isEnabled } = segment;
 
 
Thank you and sorry if this email is incorrect for proposals.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss