JS Definition
Defining JavaScript Objects: Various Methods
JavaScript provides several ways to create and manipulate objects, which are central to its programming model. Here, we'll explore different methods to define objects and discuss their nuances.
1. Object Literal (Object Initializer)
The simplest and most common method to define an object:
const person =
{ firstName: "John", lastName: "Doe", age: 50, eyeColor: "blue"
};
Object literals are straightforward and provide a clear, readable syntax.
2. Using the new Keyword
Creates an object instance using the Object constructor:
const person = new Object();
person.firstName = "John";
person.lastName = "Doe";
person.age = 50;
person.eyeColor = "blue";
Although functional, using new Object() is less efficient and less readable compared to object literals.
3. Object Constructor
Defining a more complex object using a constructor function:
function Person(firstName, lastName, age, eyeColor)
{ this.firstName = firstName;
this.lastName = lastName; this.age = age;
this.eyeColor = eyeColor;}const
person = new Person("John", "Doe", 50, "blue");
This method is useful for creating multiple instances of an object with the same properties but different values.
4. Object.assign()
Used to copy values of all enumerable own properties from one or more source objects to a target object:
const target = {};
const source = { firstName: "John", lastName: "Doe" };
Object.assign(target,
source, { age: 50, eyeColor: "blue" });
This method merges objects and is particularly useful for cloning and combining objects.
5. Object.create()
Creates a new object, using an existing object as the prototype of the newly created object:
const proto = { greet()
{ console.log
(`Hello, my name is ${this.firstName}`);
}};
const person = Object.create(proto);
person.firstName = "John";person.
lastName = "Doe";person.age = 50;person.eyeColor = "blue";
This method is powerful when you need to create an object with a specific prototype.
6. Object.fromEntries()
Transforms a list of key-value pairs into an object:
const entries = [["firstName", "John"],
["lastName", "Doe"], ["age", 50], ["eyeColor", "blue"]];
const person = Object.fromEntries(entries);
Ideal for converting Map objects or arrays of key-value pairs back to objects.
JavaScript Object Methods
General Methods
- Object.assign(): Copies properties to a target object.
- Object.entries(): Returns an array of a given object's key-value pairs.
- Object.keys(): Retrieves an object's property names as an array.
- Object.values(): Obtains an object's property values as an array.
- Object.groupBy(): Categorizes objects based on a callback function.
Property Management Methods
- Object.defineProperty(): Adds or modifies a property on an object.
- Object.defineProperties(): Configures multiple properties.
- Object.getOwnPropertyDescriptor(): Retrieves property descriptors.
- Object.getOwnPropertyDescriptors(): Fetches all property descriptors.
- Object.getOwnPropertyNames(): Lists all properties of an object.
- Object.getPrototypeOf(): Accesses the prototype of an object.
Object Protection Methods
- Using const with objects prevents reassignment but not modification of property values.
- Object.preventExtensions(): Disallows adding new properties.
- Object.seal(): Prevents adding or deleting properties.
- Object.freeze(): Locks down properties, preventing any changes.
These methods and techniques provide a robust foundation for defining and managing JavaScript objects, catering to various requirements and use cases.