The Object.defineProperty() static method is used to define a new property directly on an object, or modify an existing property on an object, and returns the object. Let’s see an example to know how to define property,
1 2 3 4 5 6 7 8 9 10 11 | const newObject = {}; Object.defineProperty(newObject, 'newProperty', { value: 100, writable: false }); console.log(newObject.newProperty); // 100 // It throws an error in strict mode due to writable setting newObject.newProperty = 200; |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.