-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1-simple.js
More file actions
44 lines (35 loc) · 930 Bytes
/
1-simple.js
File metadata and controls
44 lines (35 loc) · 930 Bytes
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
'use strict';
class Product {
constructor(name, price) {
this.name = name;
this.productPrice = price;
}
get price() {
return this.productPrice;
}
}
class Collection {
constructor(name, ...products) {
this.name = name;
this.set = new Set(products);
}
get price() {
let price = 0;
for (const item of this.set) {
price += item.price;
}
return price;
}
}
// Usage
const p1 = new Product('Laptop', 1500);
const p2 = new Product('Mouse', 25);
const p3 = new Product('Keyboard', 100);
const p4 = new Product('HDMI cable', 10);
const electronics = new Collection('Electronics', p1, p2, p3, p4);
const p5 = new Product('Bag', 50);
const p6 = new Product('Mouse pad', 5);
const textile = new Collection('Textile', p5, p6);
const purchase = new Collection('Purchase', electronics, textile);
console.dir(purchase, { depth: null });
console.log(`Total is ${purchase.price}`);