Skip to content

Commit 51a92b3

Browse files
author
Walle Cyril
committed
remove, Object.seal Object.preventExtensions Object.freeze
1 parent 453e485 commit 51a92b3

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

js/Object.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ anObject = {};
1212
// without prototype
1313
anObject = Object.create(null);
1414

15+
// creating with prefilled properties
16+
anObject = {
17+
"key1": "value1",
18+
"key2": "value2",
19+
};
20+
1521
// assign simple
1622
anObject["key"] = "value";
1723

1824
// assign multiple
1925
Object.assing(anObject, {
2026
"key1": "value1",
21-
"key2": "value2",
27+
"key2": "value2"
2228
});
2329

2430
// get a specific value
@@ -45,8 +51,31 @@ Object.entries(anObject).forEach(function ([key, value]) {
4551

4652
});
4753

48-
// has
54+
// has a key
4955
anObject.hasOwnProperty("key");
5056

5157
// has safe, works even when anObject has a key "hasOwnProperty"
5258
Object.prototype.hasOwnProperty.call(anObject, "key");
59+
60+
// remove a value
61+
anObject["key"] = undefined;
62+
63+
// completly remove a property (key and value)
64+
delete anObject["key"];
65+
66+
// prevent future extensions
67+
Object.preventExtensions(anObject);
68+
anObject["newThing"] = 2; // Error in strict mode
69+
70+
// prevent future extensions and removals
71+
anObject["beforeSealing"] = 10;
72+
Object.seal(anObject);
73+
anObject["newThing"] = 2; // Error in strict mode
74+
delete anObject["beforeSealing"]; // Error in strict mode
75+
76+
// prevent future extensions and removals and mutations
77+
anObject["beforeSealing"] = 10;
78+
Object.freeze(anObject);
79+
anObject["newThing"] = 2; // Error in strict mode
80+
delete anObject["beforeSealing"]; // Error in strict mode
81+
anObject["beforeSealing"] = 11; // Error in strict mode

0 commit comments

Comments
 (0)