Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
158 lines (113 loc) · 3.48 KB
/
Copy pathindex.js
File metadata and controls
158 lines (113 loc) · 3.48 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// /**
// * EXAMPLE 1 - Create object and modify its properties
// */
// let myObject;
// myObject = {
// a: 10,
// b: "abc"
// };
// console.log(myObject);
// myObject.a = 15;
// console.log(myObject);
// myObject.c = true;
// console.log(myObject);
// delete myObject.b;
// console.log(myObject);
// console.log(myObject.a);
// /**
// * CHALLENGE 1
// *
// * Create variable called "myPost".
// * Initial value should be {}
// * Add property called "postTitle" and value "Object is reference type"
// * Add one more property "postLikes" with value 0
// * Add third property "shared" and set its value to "false"
// * Increase value of the "postLikes" by 1
// * Delete property "shared"
// */
// let myPost = {};
// myPost.postTitle = "Object is reference type";
// myPost.postLikes = 0;
// myPost.shared = false;
// console.log(myPost);
// myPost.postLikes = myPost.postLikes + 1;
// console.log(myPost);
// delete myPost.shared;
// console.log(myPost);
// /**
// * EXAMPLE 2
// *
// * Use "const" for object declaration
// */
// const myObject = {};
// myObject.a = true; // NO ERROR! Variable is not reassigned
// console.log(myObject);
// myObject = {
// a: true
// }; // Uncaught TypeError: Assignment to constant variable.
// /**
// * CHALLENGE 2
// *
// * Create variable "myObject" and add property "a" - 10.
// * Create another variable "copyOfMyObject" and its value should be myObject.
// * Add new property "b" with value false to the "copyOfMyObject".
// * Print to the console "myObject" and "copyOfMyObject" and interpret results.
// */
// let myObject = {
// a: 10
// };
// let copyOfMyObject = myObject;
// copyOfMyObject.b = false;
// console.log(myObject);
// console.log(copyOfMyObject);
// /**
// * EXAMPLE 3
// *
// * Bracket notation
// */
// let myObject = {
// a: true,
// b: null,
// c: 25
// };
// console.log(myObject["a"]); // true
// // console.log(myObject[a]); // Uncaught ReferenceError: a is not defined
// console.log(myObject["b"]); // null
// const propertyName = "c";
// console.log(myObject[propertyName]); // 25
// console.log(myObject["propertyName"]); // undefined
// myObject["new" + "Property" + "Name"] = "Value for dynamically computed property name";
// console.log(myObject);
// /**
// * EXAMPLE 4
// *
// * Missing properties
// */
// const myObject = {
// a: 3,
// b: true
// };
// // Code execution is not stopped
// console.log(myObject.c); // undefined
// console.log(myObject.absentProperty); // undefined
// // // Code execution is STOPPED
// // console.log(nonDeclaredVariable); // Uncaught ReferenceError: nonDeclaredVariable is not defined
// // IMPORTANT!!!
// // NEVER ASSIGN "undefined" to any property or variable. Use "null" instead
// myObject.newPropertyWithUndefinedValue = undefined;
// console.log(myObject);
// console.log(myObject.newPropertyWithUndefinedValue);
// /**
// * CHALLENGE 3
// *
// * Create object "objectWithNestedObject" with initial value {}.
// * Add property "nestedObject" with initial value {}.
// * Add property "a" with value "null" to "nestedObject". Use dot notation
// * Add property "b" with value "true" to "nestedObject". Use bracket notation. Create new variable with property name
// */
// let objectWithNestedObject = {};
// objectWithNestedObject.nestedObject = {};
// objectWithNestedObject.nestedObject.a = null;
// const newPropertyName = "b";
// objectWithNestedObject.nestedObject[newPropertyName] = true;
// console.log(objectWithNestedObject);