-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobjects.html
More file actions
191 lines (132 loc) · 4.53 KB
/
objects.html
File metadata and controls
191 lines (132 loc) · 4.53 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!-- in this lesson: -->
<!-- ------------------- -->
<!--
Lesson 8 : Objects
1. Objects = group related values together
2. Added a score to Rock Paper Scissors
3. Built-in objects (JSON, localStorage)
4. More details (null, auto-boxing, references)
5. Shortcuts (destructuring, shorthand property shorthand method)
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Objects</title>
</head>
<body>
<!--
We can save any type of value in Object.
🟨Console.log = Method ~~~~~~~~~~~~~~~~~~~~~~~
🟩Console is an Object provided by JavaScript
🟩log is a function save inside console Object.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
🟨Math.round ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
🟩Math is Object
🟩random is function saved in Math object
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-->
<script>
//🟨Syntax Rules for object :
//🟨Create an object
const product = {
//🟩Property : Values
name: 'apple',
price: 100
}
//🟨Access a value~~~~~~~~~~~~~~~~~~~~~~
//🟩Inside the 🟨objectName.propertyName
console.log(typeof (product)); // object
console.log(product);
console.log(product.name); //🟩Dot Notation
console.log(product.price);
product.name = 'banana';
console.log(product);
//🟦If property not exist = value undefined
product.newProperty = true;
console.log(product);
//🟨Delete property~~~~~~~
delete product.newProperty;
console.log(product);
const product2 = {
name: 'shirt',
'delivery-time': '1 day',
rating: { // 🟨Nested Object
stars: 4.5,
count: 87
},
fun: function function1 () //🟨function is another type of value in javascript
{
console.log('function inside object');
}
};
console.log(product2);
console.log(product2.name);
//🟩Bracket Notation : let us use properties that don't work with dot notation
console.log(product2[ 'name' ]);
console.log(product2[ 'delivery-time' ]);
console.log(product2.rating);
console.log(product2.rating.count);
product2.fun();
console.log(typeof console);
console.log(typeof console.log);
// 🟨JSON : 🟩Syntax more universal then javascript
// javascript object notation
// a syntax
// similar to JavaScript object
// but has less features
// similar to JS object
// but with less features
// Built in JSON Object
// Convert:
// Javascript Object 👉➡ JSON
console.log(JSON.stringify(product2));
console.log(typeof JSON.stringify(product2)); //string
// Convert JSON 👉➡ JavaScript Object
const jsonString = JSON.stringify(product2);
console.log(JSON.parse(jsonString));
//🟨 local storage
console.log('hello'.length); //🟨Autoboxing
console.log('hello'.toUpperCase());
//🟨 Objects are References
const object1 = {
message: 'hello',
};
const object2 = object1;
console.log(object2); //🟩 Copy by reference
object1.message = 'Good job!';
console.log(object1);
console.log(object2);
const object3 = {
message: 'Good job!'
};
console.log(object3 === object1); //false
console.log(object2 === object1); //true
//🟨 Shortcut for objects
const object4 = {
message: 'Good job!',
price: 799
};
//const message = object4.message;
const { message,price } = object4;
console.log(message); // Good job!
console.log(price);
//🟨 Shorthand property
const object5 = {
// message: message
message,
// method: function function1 ()
// {
// console.log('method');
// }
method () //🟩Shorthand Method Syntax
{
console.log('method');
}
};
console.log(object5);
object5.method();
</script>
</body>
</html>