JavaScript Enhance Object Literals (ES6)

In this blog post, we are going to look at the enhanced object literals added to JavaScript/ES6. We are going to explore the following topics: 
  • Defining properties
  • Defining methods
  • Computed Property Names
Defining Properties
 
Before diving into the ES6 syntax, let us look back on how it was made in the old days.
  1. function exampleOfDefiningPropertiesOldWay(){  
  2.   
  3.     var firstName = "Jin Vincent",  
  4.         lastName = "Necesario";  
  5.   
  6.     var customer = {  
  7.         firstName: firstName,   
  8.         lastName: lastName  
  9.     }  
  10.   
  11.    return customer;  
  12. }  
  13.   
  14. console.log(exampleOfDefiningPropertiesOldWay());  
Output
 
{ firstName: "Jin Vincent", lastName: "Necesario" }
 
As you can see in the example above it shows how it was done before ES6. Looks good to me and very declarative in a way. However, there's a better way and ES6 comes to the rescue. Let us look at an example using ES6. 
  1. function exampleOfDefiningProperties(){  
  2.   
  3.     let firstName = "Jin Vincent",  
  4.         lastName = "Necesario",  
  5.         age = 38;  
  6.   
  7.     let customer =  {firstName, lastName, age};  
  8.   
  9.     return customer;  
  10. }  
  11.   
  12. console.log(exampleOfDefiningProperties());  
Nice, this example looks much simpler compared to the old way of defining properties.
 
Defining Methods
 
Let us see the old way of defining a method within the object literal. 
  1. var customers = {  
  2.     getCustomerInfo: function() { ... }  
  3. };   
If you did like the first example, you'll probably like this feature too. Let us see it in action. 
  1. function exampleOfDefiningPropertiesWithMethods(){  
  2.   
  3.     let genderSelect = ['M''F'];  
  4.   
  5.     let firstName = "Jin Vincent",  
  6.         lastName = "Necesario",  
  7.         gender = genderSelect[0],  
  8.         age = 38;  
  9.   
  10.     let customer =  {   firstName,   
  11.                         lastName,  
  12.                         gender,  
  13.                         age,   
  14.                         getCustomerInfo(){  
  15.                             return this.lastName + " " + this.firstName;  
  16.                         }  
  17.                     };  
  18.   
  19.     console.log(customer);  
  20.     console.log(customer.getCustomerInfo());  
  21. }  
  22.   
  23. exampleOfDefiningPropertiesWithMethods();  
Output
 
{ firstName: "Jin Vincent", lastName: "Necesario", gender: "M", age: 38, getCustomerInfo: getCustomerInfo() }
Necesario Jin Vincent
 
How's that?  It looks pretty straightforward to me and you'll probably be thinking I can no longer use the old way of doing it. But you can still if you want to.
 
Computed Property Names 
 
Before jumping to the examples, I would like to point out that the reason it is called computer property names is because it is evaluated during runtime.
 
Let us see the previous way of doing compute property names.
  1. function exampleOfComputerPropertyNamesOldWay(){  
  2.               
  3.     var customer = {};  
  4.   
  5.     customer["first" + "Name"] = "Jin Vincent";  
  6.   
  7.     console.log(customer["first" + "Name"]);  
  8.       
  9.     return customer["firstName"];  
  10. }  
  11.   
  12. console.log(exampleOfComputerPropertyNamesOldWay());  
The example above will output two "Jin Vincent" without the quotes. 
 
As you can see, after creating the customer object, we have attached the properties to it. However; today ES6, we can add the properties while creating an object. Let us see this in action. 
  1. function exampleOfComputerPropertyNamesES6(){  
  2.     var customer = {  
  3.         ["first" +"Name"]: "Jin Vincent",  
  4.         ["last" + "Name"]: "Necesario",  
  5.         ["current" + "Age"]: 38  
  6.     };  
  7.   
  8.     return customer;  
  9. }  
  10.   
  11. console.log(exampleOfComputerPropertyNamesES6());  
Output
 
{ firstName: "Jin Vincent", lastName: "Necesario", currentAge: 38 }
 
Summary
 
In this blog post, we have discussed enhanced object literals added to JavaScript/ES6. I hope you have enjoyed this blog post, as much as I have enjoyed writing and coding the examples. Until next time, happy programming.