-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.js
101 lines (70 loc) · 1.58 KB
/
class.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
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
//class
class Car {
constructor(model){
this.model = model
this.showName = function () {
console.log(this.model)
}
}
wheels = 4
showName1(model){
console.log(model)
}
}
const vehicle = new Car("BMW")
const vehicle2 = new vehicle.__proto__.constructor("volvo")
console.log(vehicle2)
vehicle.showName()
console.log(vehicle) // true
console.log(vehicle.__proto__.showName1(vehicle.model))
console.dir(Car)
// static fields and methods (like Array.isArray())
class home {
constructor(name){
this.name = name
}
static street = "Slowackiego"
static showStreet() {
console.log(this.street)
}
}
const h1 = new home("HomeTown")
home.showStreet()
//h1.showStreet()
// private fields (incapsulation of logic)
class user {
#firstName
#lastName
constructor(firstName, lastName){
this.#firstName = firstName
this.#lastName = lastName
}
getFullName(){
return this.#firstName + " " + this.#lastName
}
}
const u1 = new user('John', "Parker")
console.log(u1.__proto__)
console.dir(user.prototype)
// super
class motorbike {
constructor(name, age){
this.name = name
this.age = age
}
showName(){
console.log(this.name + " " + this.age)
}
}
class FordBike extends motorbike{
constructor(name, age, color){
super(name, age)
this.color = color
}
getDescription(){
super.showName()
}
}
const moto = new FordBike("bike 1", 10, "green")
console.log(moto)
moto.getDescription()