JavaScript ES6+ : Object Literals gets better

Follow me on twitter @jaymanpandya

When you are assigning a property of an object to a variable you can skip writing the assignment if the property name and variable name are same.

const nameOfFacility = "Swimming Pool";
 const canbeBooked = false;
 const isSlotted = false;
 const capacity = 25;

// One way of writing
 const clubFacility = {
     nameOfFacility: nameOfFacility,
     canbeBooked: canbeBooked,
     isSlotted: isSlotted,
     capacity: capacity
 }
// Another way of writing the same thing
 const clubFacility = {
     nameOfFacility,
     canbeBooked,
     isSlotted,
     capacity
 }

You can also define the methods in shorthand

// One way of writing
const facility = {
    fetchFacilityDetails: function(parameter) {

    },
    addFacilityDetails: function(parameter) {

    },
    removeFacilityDetails: function(parameter) {

    },
    addDetailsToFacility: function(parameter) {

    },
    removeDetailsFromFacility: function(parameter) {

    },
}

// Another way of writing the same thing
const facility = {
    fetchFacilityDetails(parameter) {

    },
    addFacilityDetails(parameter) {

    },
    removeFacilityDetails(parameter) {

    },
    addDetailsToFacility(parameter) {

    },
    removeDetailsFromFacility(parameter) {

    },
}

Peace ✌️

I am an UI designer with a flair for front-end development that makes any brand shine at what they do best. I am right now building amazing products for our clients both Indian and International at Interactive Digital Labs. I am part of the Design and Marketing Teams for Make WordPress and has spoken at local camps on Gutenberg as well as the importance of user experience. I am not intimidated by markup, languages, or tools: HTML to CSS, wireframing to prototyping, JavaScript to agile development, and design programs including Sketch, Adobe XD, Figma, Photoshop, Illustrator, InDesign etc. I love languages, and not just programming either. I speak English, Hindi, Marathi, and Gujarati. When I am not taking courses to improve my skills, I am trying new food, dabbling in filmmaking and photography, and running and cycling. Say hi to me on Twitter here @jaymanpandya.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.