The static method
Object.defineProperty() defines a new property directly on an
object, or modifies an existing property on an object, and returns the object.
Following are the properties present in Object.defineProperty() :-
configurable
true if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.
Defaults to false.
enumerable
true if and only if this property shows up during enumeration of the properties on the corresponding object.
Defaults to false.
A data descriptor also has the following optional keys:-
value
The value associated with the property. Can be any valid JavaScript value (number, object, function, etc).
Defaults to undefined.
writable
true if the value associated with the property may be changed with an assignment operator.
Defaults to false.
Code e.g
const object1 = {};
Object.defineProperty(object1, 'property1', {
value: 42,
writable: false
});
object1.property1 = 77;
// throws an error in strict mode
console.log(object1.property1);
// expected output: 42