Skip to content

Commit 0a0d3e8

Browse files
author
Cyril Walle
committed
2 parents 9dc418b + 51a92b3 commit 0a0d3e8

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

js/Object.js

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,81 @@
1-
/*Knowledge about Object*/
1+
/* Knowledge about Object */
22
"use strict";
3-
/*How to create an empty Object ?*/
3+
4+
/* How to create an empty Object ? */
45
let anObject;
56

6-
// same
7+
// same
78
anObject = Object.create(Object);
9+
anObject = Object();
810
anObject = {};
911

1012
// without prototype
11-
anObject = Object.create(null);
13+
anObject = Object.create(null);
14+
15+
// creating with prefilled properties
16+
anObject = {
17+
"key1": "value1",
18+
"key2": "value2",
19+
};
20+
21+
// assign simple
22+
anObject["key"] = "value";
23+
24+
// assign multiple
25+
Object.assing(anObject, {
26+
"key1": "value1",
27+
"key2": "value2"
28+
});
29+
30+
// get a specific value
31+
anObject["key"];
32+
33+
// get Length
34+
Object.keys(anObject).length;
35+
Object.values(anObject).length;
36+
Object.entries(anObject).length;
37+
38+
// iterate over
39+
// keys only
40+
Object.keys(anObject).forEach(function (key) {
41+
42+
});
43+
44+
// values only
45+
Object.values(anObject).forEach(function (value) {
46+
47+
});
48+
49+
// keys and values
50+
Object.entries(anObject).forEach(function ([key, value]) {
51+
52+
});
53+
54+
// has a key
55+
anObject.hasOwnProperty("key");
56+
57+
// has safe, works even when anObject has a key "hasOwnProperty"
58+
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

js/Set.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ s.add(6);
2424
s.add(7);
2525

2626
let l = listFromSet(s);
27-
let l2 = listFromSet2(s);
28-
let l3 = listFromSet3(s);
29-
30-
console.log(s, l, l2, l3);
27+
let l2 = listFromSet2(s);
28+
let l3 = listFromSet3(s);
29+
let l4 = [...s];

js/tricks.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
## `Object.assign` is also useful on arrays
44

55
```
6-
const initialScores = [0, 0, 0];
7-
// player a, player b, total games
8-
const scores = [5, 2, 7];
6+
const initialScores = [0, 0];
7+
// player a, player b, total games played
8+
const state = [5, 2, 7];
99
10-
// reset scores
11-
Object.assign(scores, initialScores);
10+
// reset scores only
11+
Object.assign(state , initialScores);
12+
// state = [0, 0, 7];
1213
```
1314

1415
## `{}` can be used to create local scopes anywhere

0 commit comments

Comments
 (0)