JavaScript .defineProperties()
The .defineProperties() method defines new or modifies existing properties directly on an object or adds multiple properties to an object at once. This method is often used for property descriptors and fine-grained control over object properties.
Syntax
Object.defineProperties(object, descriptors)
object: The object to define or modify its properties.descriptors: An object where each key represents a property name, and the corresponding value is the property descriptor. They have four optional keys:value: Specifies the value of the property.writable: A Boolean indicating whether the value of the property can be changed.get: A function that serves as a getter for the property, called when the property is accessed.set: A function that serves as a setter for the property, called when the property is assigned a new value.
Example
In this example, Object.defineProperties defines two properties (property1 and property2) on the myObject object. Each property has a descriptor object specifying attributes like value and writable:
const myObject = {};Object.defineProperties(myObject, {property1: {value: 42,writable: true,},property2: {value: 'Hello',writable: false,},});console.log(myObject.property1);console.log(myObject.property2);// Trying to modify the objectmyObject.property1 = 100; // Works because property1 is writablemyObject.property2 = 'World'; // Doesn't work because property2 is not writableconsole.log(myObject.property1);console.log(myObject.property2);
This will return the following output:
42Hello100Hello
Codebyte Example
In this example, computedProperty is not a direct property with a fixed value. Instead, it is dynamically computed based on the private variable _value, and the get and set functions control its behavior. Try running the code block below to understand better how the .defineProperties() method works:
Note: This method is used when customizing the behavior of object properties or ensuring certain constraints on them.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours