Object in ES6

The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in the same way as a dictionary and hash map. The object helps us to determine the procedure of the data types in JavaScript. We can easily present the complex or more than one value with the help of objects. It is not possible with primitive data types.

We can define various types of values, such as scalar value or array of objects or functions. The object contains the unordered data and stores of values. An object can be defined with the help of curly braces ({…}) associated with the properties list (It is optional).

The property is a “key: value” pair in which the key is a property name or string, and the value can be anything.

Syntax

There are following two ways to create an object(empty).

  • With the help of object constructor
  • With the help of object Literal

Object Literal Syntax Extension in ES6

The Object also has a literal syntax like primitive data types. The Object literal is the most common way to make an object in JavaScript programming language. ES6 provides various ways of expanding syntax, which makes the object literal more precise and robust.

Object Property Initializer

In ES5, the object literal is considered as the set of name­-value pairs.

Syntax in ES5

In the above syntax, we have created a function person () having two arguments name and age.  It returns a new object literal that contains two properties name and age. These two properties hold the values of function parameters.

 There is a drawback in the above syntax. The property’s name and age are repeated twice in keys and values. The ES6 removes the imitation if the object property and local variable have the same name.

We will try to illustrate the same syntax in ES6.

Syntax in ES6

In the above syntax code, the JavaScript engine refers to the value of arguments (name and age) to properties (name and age).

 Example

Here, we have an example of creating the object literal with the help of a local variable.

Output

After the successful execution of the example, we got the following output:

Object in ES6

Concise Method Syntax

Before ES6, when we use to define a method for an object, we must have to mention the name and definition of the complete function.

Example1

Here, we have an example to understand the concise syntax method.

In ES6, we can use short syntax, which is also known as concise method syntax. It helps us to make object literal method concise by discarding the colons (:) and the function keyword.

Here, we have another example to implement the same-

Example2

Object Cloning

The term ‘cloning’ can be defined as a mechanism of rewriting the object from one variable to another variable. We can use object.assign() method to clone an object.

Example

Here, we have an example to understand the object cloning.

Output

Object in ES6

Computed Property Name

Before ES6, the square brackets [] were used to use computed property method for the object properties. We used string literals and variables as the property name. ES6 provides a new feature “Computed property name,” which is a part of object literal syntax. This feature is used to insert the expression into the square brackets for computationand later used as the computed property name.

Example (In ES5)

Here, we have an example to understand the computed property name.

Output

After the execution of the above code, we got the following output:

Object in ES6

Example (In ES6)

Output

After the execution of the above example, we got the following output:

Object in ES6

Merge Objects in ES6

We can merge two objects in ES6 with the help of following two ways-

  • Object spread syntax method
  • Object.assign() method

Now, we are going to describe the above two methods in detail.

Object spread syntax method

This method is mostly used in a variable array where we need to store multiple values. In ES6, the object can be considered as the key-value pair entities.They can easily combine into a single entity with the help of the spread operator.

Syntax

Example

Output

After the successful execution of the above code, we got the following output:

Object in ES6

Object.assign() method

This method is used to merge multiple objects. Object.assign() method is used for copying the properties and values from one object to another target object. This method returns the target object associated with the copied properties and values.

Syntax

Example

 Output

After the execution of the above example, we got the following output:

Object in ES6

Delete Properties

We can easily delete or remove the object properties. It can be done by using the delete operator.

Example

Here, we have an example to delete the property of an object with the help of delete operator.

Output

After the execution of the above program code, we got the following output:

Object in ES6

Pin It on Pinterest

Share This