-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathes6-objects.js
More file actions
41 lines (37 loc) · 931 Bytes
/
es6-objects.js
File metadata and controls
41 lines (37 loc) · 931 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
class Square {
constructor (size) {
this.name = 'square'
this.size = size
}
area () { return this.size * this.size }
perimeter () { return 4 * this.size }
}
const sq = new Square(3)
console.log(`sq name ${sq.name} and area ${sq.area()}`)
class Circle {
constructor (radius) {
this.name = 'circle'
this.radius = radius
}
area () { return Math.PI * this.radius * this.radius }
perimeter () { return 2 * Math.PI * this.radius }
}
class Rectangle {
constructor (width, height) {
this.name = 'rectangle'
this.width = width
this.height = height
}
area () { return this.width * this.height }
perimeter () { return 2 * (this.width + this.height) }
}
const everything = [
new Square(3.5),
new Circle(2.5),
new Rectangle(1.5, 0.5)
]
for (let thing of everything) {
const a = thing.area()
const p = thing.perimeter()
console.log(`${thing.name}: area ${a} perimeter ${p}`)
}