JavaScript · Middle · Question 33 of 75
What does Object.defineProperty(obj, 'id', { value: 1, writable: false }) give you?
- ✓Correct answer: The property can't be overwritten; by default it's also non-enumerable
- 2The property can't be read from outside; it becomes private
- 3The property is removed once the object is no longer used
Explanation
Every property has writable, enumerable and configurable flags. With defineProperty, unspecified flags default to false, so the property won't appear in Object.keys and can't be deleted. Getters and setters are defined this way too.