JavaScript(ES6) Object Literal Enhancement Last Updated : 03 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Object literal enhancement is used to group variables from the global scope and form them into javascript objects. It is the process of restructuring or putting back together. Example 1: javascript <script> // variable declaration var name = "Duke"; var color = "Brown"; var age = 5; // Using Object Literal Enhancement // Combines all variables into a dog object var dog = {name, color, age}; console.log(dog); </script> Output : The name, color and age are now keys of dog object. { name:"Duke", color:"Brown", age:5 } Example 2: We can also create object methods with object literal enhancement. javascript <script> // variable declaration var name = "Tike"; var color = "Black"; var age = 7; // function declaration var bark = function(){ console.log("Woof Woof!!"); } // Using Object Literal Enhancement // combines all variables into an anotherDog object var anotherDog = {name, color, age, bark}; anotherDog.bark(); </script> Output: Woof Woof!! Example 3: We can also use "this" keyword to access the object keys. javascript <script> // Variable declaration var name = "Lilly"; var color = "White"; var age = 3; // function declaration // using "this" keyword to access the object keys. var barkWithName = function(){ console.log('Woof Woof!!, I am ' +this.name+' and I am a ' +this.age+' years old, ' +this.color+ ' coloured dog.Woof Woof!!'); } // Using Object Literal Enhancement // combines all variables into a yetAnotherDog object var yetAnotherDog = {name, color, age, barkWithName}; yetAnotherDog.barkWithName(); </script> Output : Woof Woof!!, I am lilly and I am a 3 years old, white coloured dog.Woof Woof!! Example 4: When defining object methods, it is no longer necessary to use the function keyword. Object literal enhancement allows us to pull global variables into objects and reduces typing by making the function keyword unnecessary. javascript <script> // Old syntax var driver1 = { name: "John", speed: 50, car:"Ferrari", speedUp: function(speedup){ this.speed = this.speed + speedup; console.log("new speed = "+ this.speed) } } // New syntax without function keyword const driver2 = { name: "Jane", speed: 60, car:"McLaren", speedUp(speedup){ this.speed = this.speed + speedup; console.log("new speed = "+ this.speed) } } </script> Comment More infoAdvertise with us Next Article JavaScript Object Constructors C coe17b024 Follow Improve Article Tags : JavaScript Web Technologies ES6 Similar Reads JavaScript Object fromEntries() Method The Object.fromEntries() method in JavaScript is a standard built-in object which is used to transform a list of key-value pairs into an object. This method returns a new object whose properties are given by the entries of the iterable. Syntax: Object.fromEntries( iterable ) Parameters: This method 2 min read JavaScript Object create() Method JavaScript object.create() method is used to create a new object with the specified prototype object and properties. Object.create() method returns a new object with the specified prototype object and properties.Syntax:Object.create(prototype[, propertiesObject])Parameters:prototype: It is the proto 3 min read JavaScript Object defineProperties() Method The Object.defineProperties() method in JavaScript is a standard built-in Object that defines a new or modifies existing properties directly on an object and it returns the object.Syntax:Object.defineProperties(obj, props) Parameters:Obj: This parameter holds the object on which the properties are g 2 min read JavaScript Object Accessors There are two keywords that define the accessors functions: a getter and a setter for the fullName property. When the property is accessed, the return value from the getter is used. When a value is set, the setter is called and passed the value that was set. JavaScript Getter (The get Keyword)Exampl 2 min read JavaScript Object Constructors An object is the collection of related data or functionality in the form of key. These functionalities usually consist of several functions and variables. All JavaScript values are objects except primitives. const GFG = { subject : "programming", language : "JavaScript",}Here, subject and language a 4 min read JavaScript Object defineProperty() Method The Object.defineProperty() method in JavaScript is a Standard built-in object which defines a new property directly on an object or it can also modify the existing property of an object and return the object. Syntax:Object.defineProperty(obj, prop, descriptor)Parameters:This method accepts three pa 3 min read Like