Destructure / Easy Object Composition

Posted on
  • Hey guys!

    It seems that destructuring assignment and composition is not supported in Espurino...is that true? Or is there a way to enable it? (Destruturing: https://developer.mozilla.org/en-US/docs­/Web/JavaScript/Reference/Operators/Dest­ructuring_assignment)

    For example, this doesn't work:

    const someObject = { a: 1, b: 2 }
    const { a, b } = someObject; // syntax errors on this line
    const a = someObject.a; // ...but this does work
    

    Similarly, this doesn't work:

    const a = 1, b = 2;
    const someObject = { a, b }; // syntax errors here
    someObject.a = a; // ... but this works
    

    Any plans to support these styles of operations?

  • Sat 2021.01.30

    Hi @JosiahBryan,

    'destructuring assignment and composition is not supported in Espurino'

    That would be correct according to this reference beneath table ES6

    http://www.espruino.com/Features



    From the MDN link in post#1

    'The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.'

    Were you after the exact Destruturing syntax shown in post #1, or would a similar JSON object notation work for the task at hand?   ref: properties from objects

    const someObject = { 'a': 1, 'b': 2 };
    const a = someObject.a;
    console.log ( "Value a: " + a );
    
    // output
    >Value a: 1
    

    Note that use of dump() will show the same as:

    const someObject = { a: 1, b: 2 };
    const a = someObject.a;
    console.log ( "Value a: " + a );
    
    // output
    >Value a: 1
    


    re: 'Any plans to support these styles of operations?'

    We'll need to defer to @Gordon for the official plan. . . .

  • Yes, you're right - there's no support at the moment. Generally the state of language support is on the http://www.espruino.com/Features page as Robin says.

    However nothing stops you from transpiling your JS code into something else. For instance if you go to Minification in settings and turn it to Closure (online) - Simple Optimisations you can then use destructuring.

    There's a wishlist here which you can vote on: https://github.com/espruino/Espruino/iss­ues/1302

    And generally I do try and add the most requested features when I get time :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Destructure / Easy Object Composition

Posted by Avatar for JosiahBryan @JosiahBryan

Actions