Select Page
JavaScript ES6+ : Object Literals gets better
Jayman Pandya
May 8, 2020

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 ✌️

Let's Talk

If you liked what you have seen, then let’s connect and make something amazing together.

Email me at converse@jaymanpandya.com or send a DM on my twitter @jaymanpandya