Best Website-BuildersBest Website-Builders
    What's Hot

    The Best Audiophile Gear (2023): Headphones, Speakers, Amps, DACs

    March 10, 2023

    Traute Lafrenz, the last of the White Rose anti-Nazi resistance, dies aged 103

    March 10, 2023

    Windows 11 Preview shows File Explorer ready to suggest the next file to open

    March 10, 2023
    Facebook Twitter Instagram
    Facebook Twitter Instagram
    Best Website-BuildersBest Website-Builders
    • Home
    • CSS

      Stingray device illegally deployed by ICE, Secret Service, according to DHS Surveillance Report

      March 10, 2023

      Jamie Berry Announced as President of Evolver Legal Services

      March 9, 2023

      Parent background visible only from child elements – HTML & CSS – SitePoint Forums

      March 9, 2023

      National Assembly amends standing order to allow CS to attend House of Commons from 23 March » Capital News

      March 9, 2023

      Apple Releases Safari Technology Preview 165 – Brings Bug Fixes and Performance Improvements

      March 9, 2023
    • Joomla

      Web Content Management Systems Market Business Growth Potential 2023-2030

      March 6, 2023

      How to create a successful content strategy framework

      March 3, 2023

      Free Website Hosting Services for Efficient and Reliable Work

      March 2, 2023

      Bluehost Review 2023 – Is It the Fastest Hosting Service?

      March 2, 2023

      Intermediate PHP Developer – IT-Online

      March 1, 2023
    • PHP

      Susana Morales’ family calls for police accountability

      March 10, 2023

      Sheana Shay’s lawyer denies Sheana hit Raquel

      March 10, 2023

      Tennessee Lieutenant Governor Randy McNall comments on men’s thirst trap

      March 9, 2023

      Man charged with spray-painting ‘groomers’ in library, charged with child pornography

      March 9, 2023

      TikTok users are experimenting with M&Ms as eyeshadow and more prison makeup tips

      March 9, 2023
    • UX

      Flipper Zero device seized by Brazilian Telecommunications Authority

      March 10, 2023

      Imagine looking at your job postings on LinkedIn and being paid $32,000 to $90,000 more than you earn.

      March 10, 2023

      SNAP participants in all 53 states and territories were finally able to get their stolen benefits reimbursed, and consumer complaints about credit reporting issues increased 96% in one year.

      March 9, 2023

      What Ethereum’s Latest Rollout Means for ETH and Its Roadmap

      March 9, 2023

      BMW’s iDrive 8.5 updated for smartphone-like user experience

      March 9, 2023
    • Web Builders
      1. Web Design
      2. View All

      What Comes First in Website Development — Design or Copy?

      February 2, 2023

      Modern Campus Honors Best Higher Education Websites of 2022

      February 2, 2023

      Premier SEO Consultant in Las Vegas, Nevada with Unparalleled Customer Service

      February 2, 2023

      Can Religious Freedom Be Saved? This group is racing the clock to teach America’s first freedom

      February 2, 2023

      How i Create New Google Account

      February 7, 2023

      CWT powers tools for meeting and event planners

      January 31, 2023

      Best Website Builder – Website Builders

      January 24, 2023

      Is There A Market For Rap-Themed Slot Games? – Rap Review

      January 19, 2023
    • WordPress

      Windows 11 Preview shows File Explorer ready to suggest the next file to open

      March 10, 2023

      Lenovo’s new Thinkstation PX workstation has up to 120 CPU cores but lacks key features

      March 10, 2023

      Intel breaks Cinebench R23 world record with its ultra-powerful Sapphire Rapids chip

      March 9, 2023

      Sorry Gamers, Steam Deck 2 Is A Long Way Ahead

      March 9, 2023

      There’s another really good reason not to illegally stream movies online.

      March 9, 2023
    • Realtoz
      • Our Other Sites
    • More News
    Best Website-BuildersBest Website-Builders
    Home » Deconstructing ES6 Objects and JavaScript
    JavaScript

    Deconstructing ES6 Objects and JavaScript

    websitebuildersnowBy websitebuildersnowJuly 12, 2022No Comments5 Mins Read
    Facebook Twitter LinkedIn Telegram Pinterest Tumblr Reddit WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    Angular and JavaScript
    In recent years, deconstructed food has become popular among gourmets, in which a chef breaks down a dish into core ideas (deconstruction) and reconstructs it from its basic elements (reconstruction). What does this have to do with JavaScript? It also happens to support deconstruction and reconstruction of arrays and objects. Until the release of ECMAScript 6 (ES6), the decomposition part of the equation was much more difficult than the reconstruction, requiring a few lines of code. Now, the ability to deconstruct an array or object in a single line of code offers a wealth of coding possibilities. In this web development tutorial of his, today we’ll focus on object decomposition. In the next article, we’ll focus on compound (object and array) decomposition and its more advanced usage.

    Want to learn web development in an online course format? Check out our list of the best HTML and web development courses.

    Basics of deconstructing JavaScript

    The Advanced TypeScript/ES6 Array Features article touched on Array destructuring and compared the new ES6 syntax to that of previous versions of JavaScript (JS). As an example, I assigned the elements of the array to four separate variables.

    // in pre-ES6 Javascript
    var ivoryKnightMembers = ['John', 'Rob', 'George', 'Steve'];
    var john   = ivoryKnightMembers[0], 
        rob    = ivoryKnightMembers[1],
        George = ivoryKnightMembers[2], 
        Steve  = ivoryKnightMembers[3];
    
    // using ES6 destructuring
    let ivoryKnightMembers = ['John', 'Rob', 'George', 'Steve'];
    let [john, rob, george, steve] = ivoryKnightMembers;
    

    That JavaScript tutorial defined destructuring as a feature of EcmaScript 2015 and Typescript that allows the structure of an entity to be split. While that definition was sufficient in the context of the subject at hand, it omitted several other points about decomposition, such as:

    • It can be applied to complex structures (i.e. arrays or objects).
    • Can be used for both variable assignment and/or variable declaration.
    • It supports nested structured syntax to handle nested structures.

    As it applies to objects, let’s cover each of these points in turn.

    Example of deconstructing an object in JavaScript

    The code snippet above is an example of array partitioning on variable assignment. Since JS objects store their attributes as associative arrays, you can also put object literals on the left side of assignment expressions that deconstruct objects.

    const band = {
        drummer: 'George',
        bassist: 'Steve',
        guitarist: 'Rob',
        vocalist: 'John'
    };
    
    // Object Destructuring
    const { drummer, bassist, guitarist, vocalist } = band;
    
    // Outputs "George, Steve, Rob, John"
    console.log(drummer, bassist, guitarist, vocalist); 
    

    Assigning new values ​​to local variables

    The following snippet shows how to use object decomposition to assign new values ​​to local variables. Note that you must use a pair of parentheses (()) in an assignment expression. Otherwise, an error is thrown because the split object literal is scoped as a block statement and a block cannot appear to the left of an assignment expression.

    // Initialize local variables
    let drummer="George";
    let bassist="Steve";
    let guitarist="Rob";
    let vocalist="John";
    
    const band = {
        drummer: 'George',
        bassist: 'Steve',
        guitarist: 'Rob',
        vocalist: 'John'
    };
    
    // Reassign firstname and lastname using destructuring
    // Enclose in a pair of parentheses, since this is an assignment expression
    ({ drummer, guitarist } = band);
    
    // bassist and vocalist remain unchanged
    // Outputs "George, Steve, Rob, John"
    console.log(drummer, bassist, guitarist, vocalist);  
    

    Assigning and deconstructing default values ​​in JS

    Destructuring assignment is very flexible and allows assigning variables that do not correspond to any key in the unstructured object. If you do, JS will create the variable and assign it a value without issue. undefinedOtherwise you can assign the default value yourself like this:

    const band = {
        drummer: 'George',
        bassist: 'Steve',
        guitarist: 'Rob',
        vocalist: 'John'
    };
    
    // Assign default value of 'Allan' to keyboardist if undefined
    const { drummer, bassist, guitarist, keyboardist="Allan", vocalist } = band;
    
    // List band members using ES6 template literals
    // Outputs "Ivory Knight are George, Steve, Rob, Allan, and John"
    console.log(`Ivory Knight are ${drummer}, ${bassist}, ${guitarist}, ${keyboardist}, and ${vocalist}.`);
    

    read: Using JavaScript variables and built-in functions

    Renaming Variables in JavaScript

    You probably already thought that assigning variables with destructuring is a very powerful thing. Well, it gets even better. Web developers are not limited to variables with the same name as the corresponding object key.A programmer can assign to another variable name using the syntax [object_key]:[variable_name]Want to set some default values? You can assign some using the syntax [object_key]:[variable_name] = [default_value]:

    const band = {
        drummer: 'George',
        bassist: 'Steve',
        guitarist: 'Rob',
        vocalist: 'John'
    };
    
    // Assign default value of 'Allan' to keyboards if undefined
    const { drums: drummer, bass: bassist="New guy", guitars: guitarist, keyboards="Allan", vocals: vocalist } = band;
    
    // List band members using ES6 template literals
    // Outputs "Ivory Knight are George, New guy, Rob, Allan, and John"
    console.log(`Ivory Knight are ${drums}, ${bass}, ${guitars}, ${keyboards}, and ${vocals}.`);
    

    Exploding Nested Objects in JavaScript

    As you may have noticed, objects can themselves contain other objects. So it makes sense that child objects can be unstructured as well. Here’s an example showing how to do this:

    const band = {
        drummer: 'George',
        bassist: 'Steve',
        guitarist: 'Rob',
        vocalist: 'John',
        backupVocals: {
          lowBackups: 'Steve',
          highBackups: 'Rob'
        }
    };
    
    // Assign to local variables
    const { drummer, bassist, guitarist, vocalist, backupVocals: {lowBackups, midBackups="N/A", highBackups} } = band;
    
    // Outputs "Backup vocals performed by Steve, N/A, Rob."
    console.log(`Backup vocals performed by ${lowBackups}, ${midBackups}, ${highBackups}.`);
    

    Final Thoughts on Exploding ES6 Objects

    In this web development tutorial, you learned how to deconstruct objects using ES6 syntax. In the next article, we’ll focus on compound (object and array) decomposition and its more advanced usage.

    While you wait, why not check out today’s code snippet demos? If you want, you can play with them.

    Learn more about JavaScript programming tutorials and software development guides.



    Source link

    Share this:

    • Tweet
    • Email
    • Pocket
    • Mastodon
    • WhatsApp
    • Telegram
    • Share on Tumblr
    • Print
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Telegram Email
    Previous ArticleDeath of longtime Star Eagle employee Linda Shell ‘hit to community’
    Next Article Introducing Pure CSS: Minimal Modular CSS Layout
    websitebuildersnow
    • Website

    Related Posts

    How to pass an array from Java to JavaScript

    March 9, 2023

    DAP — Web3’s New App Success Metrics | Eric Elliot | | The JavaScript Scene | March 2023

    March 9, 2023

    Top 10 AI Extensions for Visual Studio Code — Visual Studio Magazine

    March 8, 2023
    Add A Comment

    Leave a Reply Cancel reply

    Top Posts

    Subscribe to Updates

    Get the latest sports news from SportsSite about soccer, football and tennis.

    Advertisement
    Demo

    This website provides information about CSS and other things. Keep Supporting Us With the Latest News and we Will Provide the Best Of Our To Makes You Updated All Around The World News. Keep Sporting US.

    Facebook Twitter Instagram Pinterest YouTube
    Top Insights

    The Best Audiophile Gear (2023): Headphones, Speakers, Amps, DACs

    March 10, 2023

    Traute Lafrenz, the last of the White Rose anti-Nazi resistance, dies aged 103

    March 10, 2023

    Windows 11 Preview shows File Explorer ready to suggest the next file to open

    March 10, 2023
    Get Informed

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    © 2023 bestwebsite-builders. Designed by bestwebsite-builders.
    • Home
    • About us
    • Contact us
    • DMCA
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.