Object in ES6

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
var name = new Object();                        // Syntax with object constructor
var person = {};                                     // Syntax with 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

function person(name, age)
{
          return
{
          name: name,
          age: age
};
}

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

function person (name, age)
{
          return
{
          name,
          age
};      
}

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.

Let pname = ‘Jack’,
       page = ‘23’;
let person = {
     pname,
     page      
};
console.log(person.pname);
console.log(person.page);

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.

let person =
{
          fname: “Robin”,
          lname: “Watson”
          fullname: function()
{
          return this.fname + “ ” + this.lname;
}
};

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

let person =
{
          fname: “Robin”,
          lname: “Watson”
          fullname()
{
          return this.fname + “ ” + this.lname;
}
};

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.

let object1 =
{
          name: ‘Methew’,
          age: 32
};
let cloneobject.age = Object.assign({}, object1);
cloneobject.age = 42;
console.log(Object1);
console.log(cloneobject);

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.

var employee =
{
          id: 001,
          name: ‘Jackson’,
}
var section = ‘sec_name’;
employee[section] = ‘Production’;
console.log(employee);

Output

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

Object in ES6

Example (In ES6)

var section = ‘sec-name’;
var employee =
{
          id: 002,
          name: ‘Williams’;
          [section]: ‘Security’;
}
console.log(employee);

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

var new_object = […object1, …object2]

Example

var object1 = {01: “Hello”, 02: “ES6”};
var object2 = {03: “Welcome”, 04: “to the world”};
var object3 = {05: “of”, 06: “ES6”};
var final_object = {…object1, …object2, …object3};
console.log(final_object);

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

Object.assign(target_obj, source_obj)

Example

var object1 = {01: “Hello”, 02: “ES6”};
var object2 = {03: “Welcome”, 04: “to the world”};
var object3 = {05: “of ES6”};
// Using Object.assign()
var final_object = Object.assign(object1, object2, object3);     
console.log(final_object);

 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.

var object = new Object;
Object.p = 100;
Object.q = 200;
delete object.p;
console.log(object.p);

Output

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

Object in ES6

Related Topics

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 minutes read.

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

String in ES6

String in ES6 The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like...

9 minutes read.

Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6 The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators....

11 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.

Features of ES6 | ECMAScript 6

The ES6 or ECMAScript 2015 came with various exiting and new features. These features make it a more robust and updated programming language than ES5. Some of these new promising features are Const...

6 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Number in ES6

Number in ES6 In ES6, we have various methods and properties to implement numeric functions such as floating-point, integers, date, etc. The numbers in ES6 enable us to work with number...

7 minutes read.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

10 minutes read.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

3 minutes read.

Types of Inheritance in ES6

Types of Inheritance The term inheritance can be divided into following three parts- Single-level InheritanceMultiple InheritanceMulti-level Inheritance Single-level Inheritance The single-level inheritance can be defined as an inheritance in which the child class can...

5 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

3 minutes read.

Spread Operator in ES6

Spread Operator in ECMAScript 6 The term 'Spread Operator' can be defined as an operator that allows expending the array into separate elements. Three dots (…) isthe syntax of the Spread Operator.The Spread operator...

5 minutes read.

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

4 minutes read.

Functions in ES6 | ECMAScript 6

Functions in ECMAScript 6 In ECMAScript programming language, the term ‘function’ can be defined as a collection or group of input statements, used to perform particular computational calculations and generate the output...

9 minutes read.