-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08-objects.html
133 lines (92 loc) · 3.35 KB
/
08-objects.html
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
<!DOCTYPE html>
<head>
<title>Object</title>
</head>
<body>
<script>
/*
const product = {
name: 'socks',
price: 1090
}
console.log(product);
console.log(product.name);
console.log(product.price);
product.name = 'cotton socks';
console.log(product);
product.newProperty = true;
console.log(product);
delete product.newProperty;
*/
/*
const product2 = {
name: 'shirt' ,
['delivery-time']: '1 day',
// nested objects (object inside object)
rating: {
stars: 4.5,
count: 87
} ,
func: function function1() {
console.log("function inside object");
}
};
console.log(product2);
console.log(product2.name);
console.log(product2['name']);
console.log(product2['deliver-time']);
console.log(product2.rating.count);
product2.func();
// the following is a method. A method is a function inside an object. console.log is a method because it log is a function inside console object. Math.random is also a method.
console.log (typeof console.log);
// built in methods such as JSON and Local Storage
// JSON = JavaScript Object NOtation
// JSON is a syntax, it is Javascript Object code but with simpler syntax and less features. But it is readable by all programming languages so it is more universal so it is used when sending data between computers.
// JavaScript Object > JSON string
console.log(JSON.stringify(product2));
// JSON string > JavaSCript Object
const jsonString = JSON.stringify(product2);
console.log(JSON.parse(jsonString));
*/
// AutoBoxing: strings also have properties such as .length because in javascript strings and numbers are Automatically wrapped around with special objects like boxes.
console.log('hello'.length);
console.log('jello'.toUpperCase());
// Objects are references:
const object1 = {
message: 'hello'
};
// copy by reference
const object2 = object1;
object1.message = 'Good Job!';
console.log(object1);
console.log(object2);
const object3 = {
message: 'Good Job!'
};
// can not directly compare objects because here the references are being compared since object1 and 3 have different references they are shown not equal but since object 2 and 1 have same reference they are shown equal
console.log(object3 === object1);
console.log(object2 === object1);
const object4 = {
message: 'Good Job!' ,
price: 799
}
//const message = object4.message
//^^ shortcut for when variable and object name are the same:
// destructuring shortcut
const {message, price} = object4;
console.log(price);
const object5 = {
message ,
// ^^ shorthand property: same as message: message
//method: function function1() {
// console.log('method');
//}
method() {
console.log('method');
}
};
console.log(object5);
object5.method();
</script>
</body>
</html>