There are two types of creating object in javascript:
- declarative (
const a = {}
) - constructed (
const a = new Object()
)
There are sub-types of object in javascript:
String
, Number
, Boolean
, Function
,
Array
, Date
, RegExp
, Error
var myObject = {};
Object.defineProperty( myObject, "a", {
value: 2,
writable: false, // not writable!
configurable: true,
enumerable: true
} );
myObject.a = 3;
myObject.a; // 2 or TypeError in strict mode
Object.defineProperty( myObject, "a", {
value: 2,
writable: true,
configurable: false,
enumerable: true
} );
myObject.a; // 2
delete myObject.a;
myObject.a; // 2
Set enumerations ability, such as the for..in
By combining writable:false
and configurable:false
,
you can create a constant (cannot be changed, redefined or deleted) object property
var myObject = {
a: 2
};
Object.preventExtensions( myObject );
myObject.b = 3;
myObject.b; // undefined
Object.seal(..) creates a "sealed" object, which means it takes an existing object and calls Object.preventExtensions(..) on it, but also marks all its existing properties as configurable:false.
- Add more properties
- Reconfigure properties
- Delete properties
- Modify properties values
Object.freeze(..) creates a frozen object, which means it takes an existing object and calls Object.seal(..) on it, but it also marks all "data accessor" properties as writable:false.
- Add more properties
- Reconfigure properties
- Delete properties
- Modify properties values