Object.defineProperty()
ES5+Defines a new property directly on an object, or modifies an existing property.
Syntax
Object.defineProperty(obj, prop, descriptor)Parameters
obj Object The object on which to define the property
prop string The name of the property to define
descriptor Object The descriptor for the property being defined
Return Value
Object
The object that was passed to the function
Examples
JavaScript
const obj = {};
Object.defineProperty(obj, 'x', {
value: 42,
writable: false
});
console.log(obj.x);
obj.x = 100;
console.log(obj.x); Output:
// 42
42