-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection4.js
101 lines (74 loc) · 2.25 KB
/
section4.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Objects
const myCity = {
name: "New York",
population: 8419600,
area: 468.9,
};
const alternativeOfMYCity = {
//myCity
area: 468.9,
name: "New York",
population: 8419600,
};
// Both objects are technically equal, because order of object in JS doesn't matter.
delete myCity.area; // "delete" operator removes the property from the object
myCity.name = "Los Angeles"; // name = Los Angeles
// Using dot notation You can access or create new properties in the object.
console.log(myCity);
// TODO Bracket notation
const myCity2 = {
name: "Houston",
};
const myCity2Property = "population";
myCity2[myCity2Property] = 2000000; // myCity2[population] = 2000000
console.log(myCity2);
// Using bracket notation You can access or create new properties in the object. You can also use another variable as a property name.
// TODO Nested property
const myCountry = {
name: "Finland",
info: {
isCold: true,
isLandlocked: false,
},
};
delete myCountry.info.isLandlocked; // delete property from the object
console.log(myCountry);
//TODO Object destructuring
const name = "Vladimir";
const age = 72;
const personStats = {
name: name,
age: age,
isSick: true,
};
const shortenedPersonStats = {
name, // shortened syntax. Is recommended to use in the beggining of the code
age,
isSick: true,
};
console.log(shortenedPersonStats);
//TODO window is a global object in the browser
// globalThis is a global object in Node.js. In modern WebBrosers, globalThis is also available.
const myObject = {
greeting: function () {
console.log("Hello!");
},
};
myObject.greeting();
//TODO myCity.city - access to the property. If I will add (), the error will occur "city is not a function"
// myCity.city() - call the method]
//TODO JSON
JSON.stringify(myObject); // convertation to JSON
console.log(myObject); // { greeting: [Function: greeting] }
const myObjectStringified = JSON.stringify(myObject);
JSON.parse(myObjectStringified); // { greeting: "Hello!" } convertation to JavaScript
//TODO Mutation of the object
const Japanese = {
kanji: "2200",
hiragana: "46",
katakana: "46",
};
const JapaneseCopy = Japanese; // copy by reference
JapaneseCopy.kanji = 4600;
JapaneseCopy.pronunciation = "Dakuten + Handakuten";
console.log(Japanese);