JavaScript: Prevent Adding Property
What is Extensible?
- Objects have internal slot βIsExtensibleβ that indicates if properties can be added to the object.
- the Property Attribute
configurable
determines if a property can be deleted. - the Property Attribute
writable
determines if a property value can be changed.
together, they determine if property can be added, deleted, or value changed.
Warning: parent object may be extensible
Note: if a object is not extensible, but its parent may be, so people can add properties to the parent object, and your object may still get unexpected properties, because of inheritance.
What Objects Are Extensible?
- User-defined objects are extensible, by default.
- Standard objects (For example, Object, Array, Function, Date, etc) are extensible, by default.
- Host objects (for example,
windows.document
) may or may not be extensible.
console.log( [Object, Array, Function, String, Date, RegExp].every((x) => Object.isExtensible(x) ), );
[see Object Type]
Check If Object is Extensible
Prevent Adding Properties
Prevent Adding/Deleting Properties
Prevent Adding/Deleting/Writing Properties
JavaScript Prevent Change Property
JavaScript Object and Inheritance
- Object Basics
- Object Overview
- Object Type
- Test If a Value is Object Type π
- Find Object's Type
- Prototype and Inheritance
- Prototype Chain
- Is in Prototype Chain?
- Get/Set Parent
- Show Parent Chain π
- Create Object
- Object Literal Expr
- Create Object + Parent
- Prevent Adding Property
- Clone Object π
- Test Object Equality π
- Add Method to Prototype