-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
62 lines (53 loc) · 1.37 KB
/
example.js
File metadata and controls
62 lines (53 loc) · 1.37 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
// Class definition
class Vehicle {
constructor(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
getInfo() {
return `${this.year} ${this.make} ${this.model}`;
}
}
// Inheritance
class Car extends Vehicle {
constructor(make, model, year, doors) {
super(make, model, year);
this.doors = doors;
}
// Async method example
async startEngine() {
return new Promise(resolve => {
setTimeout(() => {
resolve(`${this.getInfo()} engine started`);
}, 1000);
});
}
}
// Array methods and arrow functions
const vehicles = [
new Car('Toyota', 'Camry', 2020, 4),
new Car('Honda', 'Civic', 2019, 4),
new Car('Tesla', 'Model 3', 2021, 4)
];
// Async/await example
async function testVehicles() {
try {
for (const vehicle of vehicles) {
const result = await vehicle.startEngine();
console.log(result);
}
} catch (error) {
console.error('Error:', error);
}
}
// Object destructuring
const { make, model } = vehicles[0];
console.log(`First vehicle: ${make} ${model}`);
// Map and filter examples
const vehicleInfo = vehicles
.filter(v => v.year >= 2020)
.map(v => v.getInfo());
console.log('Newer vehicles:', vehicleInfo);
// Run async function
testVehicles();