JS Coding Questions Logo
JS Coding Questions
#218💼 Interview💻 Code

How do you define property on Object constructor

Advertisement

728x90

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,

javascript
1const newObject = {};
2
3Object.defineProperty(newObject, "newProperty", {
4  value: 100,
5  writable: false,
6});
7
8console.log(newObject.newProperty); // 100
9
10newObject.newProperty = 200; // It throws an error in strict mode due to writable setting

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 48

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
218of476