Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Property Descriptors | Understanding and Working with Objects
Objects and Prototypes in JavaScript

bookProperty Descriptors

Property descriptors in JavaScript provide fine-grained control over the behavior of individual object properties. Each property on an object has associated attributes, known as property descriptors, that determine how the property can be used. The four main descriptors are configurable, enumerable, writable, and value.

  • configurable: determines whether the property can be deleted or its descriptors can be changed;
  • enumerable: controls whether the property appears during enumeration of the object's properties, such as in a for...in loop;
  • writable: specifies if the value of the property can be changed;
  • value: holds the actual data stored in the property.

Understanding these descriptors allows you to define and manage object properties with precision, enabling patterns such as immutability, hidden properties, and more.

123456789101112131415161718192021
const user = {}; Object.defineProperty(user, "id", { value: 101, writable: false, enumerable: false, configurable: false }); // Try to change the value user.id = 202; // Try to enumerate properties for (let key in user) { console.log(key); // "id" will not be logged } // Try to delete the property delete user.id; // returns false console.log(user.id); // 101
copy

In this example, you use Object.defineProperty to add an id property to the user object. The property descriptors set for id have important effects:

  • Setting writable to false means attempts to change user.id will be ignored (or throw an error in strict mode), so the value remains 101;
  • Setting enumerable to false hides the property from enumeration, so id does not show up in for...in loops or Object.keys;
  • Setting configurable to false prevents the property from being deleted or its descriptors from being changed;
  • The value is set to 101, which is the actual data stored in the property.

These settings create a property that is read-only, hidden from enumeration, and cannot be deleted or reconfigured.

1234567891011
const user = {}; Object.defineProperty(user, "id", { value: 101, writable: false, enumerable: false, configurable: false }); const descriptor = Object.getOwnPropertyDescriptor(user, "id"); console.log(JSON.stringify(descriptor));
copy
question mark

What is the effect of setting the 'writable' descriptor to false for a property in a JavaScript object?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you explain what happens if I set configurable to true?

What is the difference between enumerable and writable?

How can I make a property both hidden and read-only?

Awesome!

Completion rate improved to 7.69

bookProperty Descriptors

Sveip for å vise menyen

Property descriptors in JavaScript provide fine-grained control over the behavior of individual object properties. Each property on an object has associated attributes, known as property descriptors, that determine how the property can be used. The four main descriptors are configurable, enumerable, writable, and value.

  • configurable: determines whether the property can be deleted or its descriptors can be changed;
  • enumerable: controls whether the property appears during enumeration of the object's properties, such as in a for...in loop;
  • writable: specifies if the value of the property can be changed;
  • value: holds the actual data stored in the property.

Understanding these descriptors allows you to define and manage object properties with precision, enabling patterns such as immutability, hidden properties, and more.

123456789101112131415161718192021
const user = {}; Object.defineProperty(user, "id", { value: 101, writable: false, enumerable: false, configurable: false }); // Try to change the value user.id = 202; // Try to enumerate properties for (let key in user) { console.log(key); // "id" will not be logged } // Try to delete the property delete user.id; // returns false console.log(user.id); // 101
copy

In this example, you use Object.defineProperty to add an id property to the user object. The property descriptors set for id have important effects:

  • Setting writable to false means attempts to change user.id will be ignored (or throw an error in strict mode), so the value remains 101;
  • Setting enumerable to false hides the property from enumeration, so id does not show up in for...in loops or Object.keys;
  • Setting configurable to false prevents the property from being deleted or its descriptors from being changed;
  • The value is set to 101, which is the actual data stored in the property.

These settings create a property that is read-only, hidden from enumeration, and cannot be deleted or reconfigured.

1234567891011
const user = {}; Object.defineProperty(user, "id", { value: 101, writable: false, enumerable: false, configurable: false }); const descriptor = Object.getOwnPropertyDescriptor(user, "id"); console.log(JSON.stringify(descriptor));
copy
question mark

What is the effect of setting the 'writable' descriptor to false for a property in a JavaScript object?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 4
some-alt