Object.defineProperty() - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
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.
Note: You call this method directly on the Object
constructor rather than on an instance of type Object
.
Object.defineProperty(object1, 'property1', {
value: 42,
writable: false
});
object1.property1 = 77;
// throws an error in strict mode
console.log(object1.property1);
// expected output: 42