You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Konstantin Smirnov (Jira)" <ji...@apache.org> on 2022/10/07 09:18:00 UTC

[jira] [Created] (GROOVY-10788) Object deconstruction a-la Javascript

Konstantin Smirnov created GROOVY-10788:
-------------------------------------------

             Summary: Object deconstruction a-la Javascript
                 Key: GROOVY-10788
                 URL: https://issues.apache.org/jira/browse/GROOVY-10788
             Project: Groovy
          Issue Type: Improvement
            Reporter: Konstantin Smirnov


In JS you can perform following operations on Arrays:
{code:java}
const [ a, b, c ] = [ 1, 2, 3 ]
{code}
which corresponds to mutli-assignment in Groovy.

However JS does some really nice syntactic sugar with Object deconstruction on top:
{code:java}
// "forward"
const object = { question:'about life', answer:42, z:11 }
const { question, answer } = object

// "backward"
const newObject = { question, answer }
{code}
This way you can save a lot of code-duplication between declaring variables and Map keys.

 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)