-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpropertydescriptor.js
51 lines (41 loc) · 1.62 KB
/
propertydescriptor.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const employee = {
firstName: "Vishal",
lastName: "Sharma",
};
let dsecriptor = Object.getOwnPropertyDescriptors(employee);
console.log(dsecriptor, "dsecriptor");
// configurable // true if this is true than value of key can be change else it will be read only
// value// the value of key
// enumerable // true // while traversing the object if this is false you will not get that propertyName
// writable // true // modification deletion
// how to update these value
// we have function called definedPropety
for (let name in employee) {
console.log(name, "here for");
}
// writable is false than you cannot change the value
// if enumrable is fale you cannot iterate that key
Object.defineProperty(employee, "firstName", {
writable: false,
enumerable: false,
// configurable: false,
});
// this will throw error you cannot update the propertyName again
// once configurable is make false you cannot reset the value of configurable key and writable key
// configurable : false means cannot modify cannot delete cannot definedProperty again
Object.defineProperty(employee, "firstName", {
configurable: true,
writable: true,
});
for (let name in employee) {
console.log(name, "here after making enumerable:false to firstName");
}
let dsecriptor1 = Object.getOwnPropertyDescriptors(employee);
console.log(dsecriptor1);
employee.firstName = "Rimo";
let dsecriptor2 = Object.getOwnPropertyDescriptors(employee);
console.log(dsecriptor2);
console.log(employee);
// whether the object property is enumerable or not
console.log(employee.propertyIsEnumerable("firstName"));
console.log(employee.propertyIsEnumerable("lastName"));