-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-objects.html
More file actions
119 lines (96 loc) · 2.85 KB
/
08-objects.html
File metadata and controls
119 lines (96 loc) · 2.85 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
<!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>
<script>
/*
//object
const product = {
//property : value
name : 'socks',
price : 1090
};
console.log(product);
console.log(product.name);
//add new object's property
product.newProperty = true;
console.log(product);
//delete property
delete product.newProperty;
console.log(product);
*/
/* LESSONS 2
const product2 = {
name : 'shirt' ,
//because - will be detect as minus
'delivery-time' : 12,
//nested
rating: {
stars : 4.5,
count : 87
},
fun : function function1(){
console.log('function is also a value');
}
};
console.log(product2['name']);
console.log(product2['delivery-time']);
console.log(product2.rating.count);
product2.fun();
Java Script Object Notation
- send data btween computers
- store data
//JSON built-in objects (syntax)
//do not support function as object's attb
//convert JS Object to JSON <<JSON.stringify>>
console.log(JSON.stringify(product2)); <-- save object in string
//convert JSON to JS Object <<JSON.parse>>
const jsonString = JSON.stringify(product2);
console.log (JSON.parse(jsonString));
//local storage (does not delete data after refresh page)
*/
//AUTO-BOXING
console.log('hello'.length);
console.log('hello'.toUpperCase());
const object1 = {
message : 'hello',
};
//copy reference (object is reference)
const object2 = object1;
object1.message = 'NICE TRY';
console.log(object1);
console.log(object2);
const object3 = {
message : 'NICE TRY',
};
//objects are reference (not compare value)
console.log(object1 === object3);
console.log(object1 === object2);
//shortcut object
const object4 = {
message : 'NICE TRY',
price : 321
};
//const message = object4.message
//take out property out of object
const {message , price} = object4;
console.log(message);
console.log(price);
const object5 = {
//message : message
//shorthand property
message,
//shorthand fx
method(){
console.log('method');
}
};
console.log(object5);
object5.method();
</script>
</body>
</html>