What is Enhanced Object Literal in Javascript?

Enhanced object literals, also known as ES6 object literals.

it is set of new features introduced in ES2015 that make it easier to create and work with objects in JavaScript.


Here are some of the key features of enhanced object literals:

Concise property initialization: You can now initialize object properties using a more concise syntax, especially when the variable names match the property names you want to assign. For example:


// ES5 code

var name = 'John Doe';

var age = 30;

var person = {

  name: name,

  age: age

};


// ES6 code

const name = 'John Doe';

const age = 30;

const person = { name, age };


Computed property names: You can now use expressions as property names, making it easier to create dynamic objects. For example:


// ES5 code

var key = 'name';

var obj = {};

obj[key] = 'John Doe';


// ES6 code

const key = 'name';

const obj = { [key]: 'John Doe' };


Method shorthand: You can now define methods directly in object literals, using a more concise syntax. For example:


// ES5 code

var obj = {

  greet: function() {

    console.log('Hello!');

  }

};


// ES6 code

const obj = {

  greet() {

    console.log('Hello!');

  }

};


I hope you would have explored something new in this post.

Thank you for visiting.

Comments

Popular posts from this blog

Bard Extensions: A new way to interact with AI

How to Increase Your Revenue as a Blogger: Strategies for Optimizing Your CPA, CPP, and CPC