JavaScript(ES6) Object Literal Enhancement Last Updated : 03 Jan, 2023 Comments Improve Suggest changes 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(ES6) Object Literal Enhancement 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 entries() Method The Object.entries() method in JavaScript is used to retrieve an array of an object's enumerable property [key, value] pairs. This method is particularly useful for transforming and iterating over objects in situations where array-like manipulation is needed.Syntax:Object.entries(obj);Parameters:obj 4 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 __defineGetter__() Method The __defineGetter__() method is used to bind an object's property to a function which will be called when the specified property is looked up. It is recommended to use the object initializer syntax or the Object.defineProperty() API instead of this method as it is being deprecated. Syntax: obj.__de 2 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 Object.create vs. New in JavaScript The new keyword and Object.create() are frequently used to instantiate objects in JavaScript but they have distinct characteristics and use cases. Understanding the differences between new and Object.create() is essential for mastering object creation in JavaScript. What is new?The new keyword in Ja 2 min read Compact Objects in JavaScript Compacting an object refers to removing properties with false values (like null, undefined, 0, false, "", NaN, etc.). Objects are used to store collections of data and more complex entities. They can be extended and manipulated in various ways to suit different programming needs. One interesting asp 3 min read Are JavaScript arrays objects? Yes, JavaScript arrays are actually specialized objects, with indexed keys and special properties. They have a length property and are technically instances of the Array constructor.JavaScriptconst a = [10, 20, 30]; console.log(typeof a);Outputobject You can add non-integer properties to arrays, mak 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 Like