Property 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...inloop; - 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.
123456789101112131415161718192021const 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
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
falsemeans attempts to changeuser.idwill be ignored (or throw an error in strict mode), so the value remains101; - Setting enumerable to
falsehides the property from enumeration, soiddoes not show up infor...inloops orObject.keys; - Setting configurable to
falseprevents 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.
1234567891011const user = {}; Object.defineProperty(user, "id", { value: 101, writable: false, enumerable: false, configurable: false }); const descriptor = Object.getOwnPropertyDescriptor(user, "id"); console.log(JSON.stringify(descriptor));
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 7.69
Property Descriptors
Swipe um das Menü anzuzeigen
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...inloop; - 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.
123456789101112131415161718192021const 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
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
falsemeans attempts to changeuser.idwill be ignored (or throw an error in strict mode), so the value remains101; - Setting enumerable to
falsehides the property from enumeration, soiddoes not show up infor...inloops orObject.keys; - Setting configurable to
falseprevents 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.
1234567891011const user = {}; Object.defineProperty(user, "id", { value: 101, writable: false, enumerable: false, configurable: false }); const descriptor = Object.getOwnPropertyDescriptor(user, "id"); console.log(JSON.stringify(descriptor));
Danke für Ihr Feedback!