-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprograms.html
More file actions
242 lines (191 loc) · 5.25 KB
/
programs.html
File metadata and controls
242 lines (191 loc) · 5.25 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Objects Programs</title>
</head>
<body>
<script>
//🟨1. Creating Objects using Object Literal Notation:
var person = {
firstName: "Deep",
lastName: "Kothari",
age: 30,
address: {
street: "123 xyz",
city: "Surat",
State: "Gujarat",
},
}
console.log(person.firstName)
console.log(person.address.city)
//🟨2. Adding and Modifying Object Properties:
person.age = 31
delete person.address
console.log(person)
//🟨3. Creating Object Methods:
var car = {
brand: "volvo",
model: "xc60",
year: 2023,
start: function () {
console.log(`Starting the ${this.brand} ${this.model}`)
},
}
car.start()
//🟨4. Object Constructors:
function Book(title, author, year) {
this.title = title
this.author = author
this.year = year
}
var book1 = new Book("Science book", "Einsteine", 1998)
var book2 = new Book("Maths book", "Newton", 1998)
console.log(book1.title)
//🟨5. Object Prototypes:
Book.prototype.getInfo = function () {
return `${this.title} by ${this.author} in year ${this.year}`
}
console.log(book1.getInfo())
//🟨6. Object Methods:
var student = {
name: "Deep",
age: 20,
greet: function () {
console.log(`Hello, my name is ${this.name}, and i am ${this.age} years old.`)
},
}
student.greet()
//🟨7. Object Iteration:
for (var key in student) {
console.log(key + ": " + student[key])
}
//🟨8. Object Destructuring:
var { name, age } = student
console.log(name)
console.log(age)
//🟨9. Object Shorthand:
var username = "Bob"
age = 25
var user = { username, age }
//🟨10. Object Spread Operator:
var defaults = { color: "blue", fontSize: "16px" }
var customStyles = { ...defaults, fontSize: "20px" }
console.log(customStyles)
//🟨11. Object Prototypes and Inheritance:
var animal = {
type: "Unknown",
sound: "Noise",
makeSound: function () {
console.log(`${this.type} makes a ${this.sound}`)
},
}
var dog = Object.create(animal)
dog.type = "Dog"
dog.sound = "Bark"
dog.makeSound()
//🟨12. Object Constructors and Classes:
class Person {
constructor(name, age) {
this.name = name
this.age = age
}
greet() {
console.log(`Hello, my name is ${this.name}, and i am ${this.age} years old.`)
}
}
var person1 = new Person("Deep", 20)
person1.greet()
//🟨13. Object.defineProperty:
var man = { name: "Deep" }
Object.defineProperty(man, "age", {
value: 30,
writable: true,
enumerable: true,
configurable: true,
})
man.age = 25
console.log(man.age)
//🟨14. Object.freeze and Object.seal:
var bike = { make: "BMW", model: "S100RR" }
Object.freeze(bike)
bike.year = 2022 // This assignment will be ignored
console.log(bike.year) //undefined
var laptop = { brand: "Apple", model: "m2" }
Object.seal(laptop)
laptop.model = "Dell" // Property modification is allowed
laptop.weight = "2lbs" // This assignment will be ignored=
//🟨15. Object.getPrototypeOf and Object.setPrototypeOf:
var person = { name: "Deep" }
var student = { major: "Compuer science" }
Object.setPrototypeOf(student, person)
console.log(student.name)
//🟨16. Object.keys, Object.values, and Object.entries:
var car = { make: "Ford", model: "mustang", year: 2023 }
console.log(Object.keys(car))
console.log(Object.values(car))
console.log(Object.entries(car))
// |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//🟨 ES6 Classes and Inheritance:
class Animal {
constructor(name) {
this.name = name
}
speak() {
console.log(`${this.name} makes a sound`)
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks`)
}
}
var dog = new Dog("Buddy")
dog.speak()
//🟨 Getters and Setters:
class Circle {
constructor(radius) {
this._radius = radius
}
get radius() {
return this._radius
}
set radius(newRadius) {
if (newRadius >= 0) {
this._radius = newRadius
}
}
get area() {
return Math.PI * this._radius ** 2
}
}
var myCircle = new Circle(5)
console.log(myCircle.radius)
myCircle.radius = 7
console.log(myCircle.radius)
console.log(myCircle.area)
//🟨 Serialization and Deserialization:
var person = {
name: "Deep",
age: 30,
hobbies: ["reading", "playing"],
}
const jsonPerson = JSON.stringify(person)
console.log(jsonPerson)
const parsedPerson = JSON.parse(jsonPerson)
console.log(parsedPerson.name)
//🟨 Mapping and Filtering Objects:
var students = [
{ name: "Jack", score: 87 },
{ name: "Oggy", score: 92 },
{ name: "Olly", score: 71 },
]
var highScores = students.filter((student) => student.score >= 90)
console.log(highScores)
var studentNames = students.map((student) => student.name)
console.log(studentNames)
</script>
</body>
</html>