DEV Community

Cover image for Understanding Property Descriptors in JavaScript
waelhabbal
waelhabbal

Posted on

1

Understanding Property Descriptors in JavaScript

As a JavaScript developer, you've probably worked with objects and properties before. But have you ever wondered how you can control the behavior of these properties? Enter property descriptors.

Property descriptors are objects that allow you to define the behavior of an object's properties. They give you fine-grained control over how properties are accessed, assigned, and deleted.

There are two types of property descriptors: data descriptors and accessor descriptors.

Data descriptors define the value of a property and whether it can be changed. They have the following properties:

  • value: The value of the property.
  • writable: Whether the value of the property can be changed.
  • enumerable: Whether the property can be enumerated (looped over) using for...in or Object.keys().
  • configurable: Whether the property can be deleted or have its attributes changed.

Accessor descriptors define functions that get or set the value of a property. They have the following properties:

  • get: A function that gets the value of the property.
  • set: A function that sets the value of the property.
  • enumerable and configurable: Same as in data descriptors.

Here's an example of how you can use property descriptors to create a read-only property:

const obj = {};
Object.defineProperty(obj, "readOnlyProp", {
  value: "foo",
  writable: false,
  enumerable: true,
  configurable: false,
});

console.log(obj.readOnlyProp); // "foo"
obj.readOnlyProp = "bar"; // Throws an error in strict mode
console.log(obj.readOnlyProp); // "foo"
delete obj.readOnlyProp; // Throws an error in strict mode
console.log(obj.readOnlyProp); // "foo"
Enter fullscreen mode Exit fullscreen mode

In this example, we use Object.defineProperty() to define a property called readOnlyProp on the obj object. We set the writable property to false, which makes the property read-only. We also set configurable to false, which prevents the property from being deleted or having its attributes changed.

Property descriptors give you a lot of power and flexibility when working with objects and properties in JavaScript. By using them, you can create properties with custom behavior that go beyond the default behavior of plain old JavaScript objects.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more