This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 268
/
08-classes.js
72 lines (59 loc) · 1.55 KB
/
08-classes.js
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
'use strict';
// Объявление класса
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return `(${this.x}, ${this.y})`;
}
}
let point = new Point(2, 1);
console.log( point.toString() ); // (2, 1)
// Наследование
class ColorPoint extends Point {
constructor(x, y, color) {
super(x, y);
this.color = color;
}
toString() {
return `${super.toString()} in ${this.color}`;
}
}
let colorPoint = new ColorPoint(4, 2, 'red');
console.log( colorPoint.toString() ); // (4, 2) in red
console.log(colorPoint instanceof ColorPoint); // true
console.log(colorPoint instanceof Point); // true
// Статические методы
class Button {
static getElementType() {
return 'button';
}
constructor(name) {
this.color = color;
}
click() {
console.log('Button clicked!');
}
}
console.log( Button.getElementType() ); // button
// Геттеры и сеттеры
class User {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// геттер
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
// сеттер
set fullName(newValue) {
[this.firstName, this.lastName] = newValue.split(' ');
}
};
let user = new User('Вася', 'Пупков');
console.log( user.fullName ); // Вася Пупков
user.fullName = 'Иван Петров';
console.log( user.fullName ); // Иван Петров