-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropertyDescriptors.js
34 lines (25 loc) · 1.22 KB
/
propertyDescriptors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
A "Property Descriptor" is a configuration record for an object property
There are two types: Data Descriptors and Accessor Descriptors
Data Descriptor: {value:<any>,writable:<bool>,enumerable:<bool>,configurable:<bool>}
Accessor Descriptor: {get(){},set(){},enumerable:<bool>,configurable:<bool>}
*/
const obj1 = {};
// define a property with a configuration
Object.defineProperty(obj1, "user", {value: "John", writable: true, configurable: true}); // unset keys default to false
// read the property descriptor
console.log(Object.getOwnPropertyDescriptor(obj1, "user")); // {value: 'John', writable: true, enumerable: false, configurable: true}
// change an attribute
Object.defineProperty(obj1, "user", {value: "Jane"});
console.log(obj1.user) // Jane
// reconfigure property
Object.defineProperty(obj1, "user", {writable: false});
obj1.user = "Ghost";
console.log(obj1.user) // Jane (writable is false so assignment has no effect)
// even if writable is false you can still use defineProperty to change the value
Object.defineProperty(obj1, "user", {value: "God"});
console.log(obj1.user) // God
// reconfigure property
Object.defineProperty(obj1, "user", {writable: true});
obj1.user = "Mary";
console.log(obj1.user) // Mary