-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11_classes.js
108 lines (83 loc) · 2.7 KB
/
11_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
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
102
103
104
105
// https://medium.com/developers-arena/javascript-classes-inheritance-and-prototype-chaining-es5-and-es6-way-4b8e9416702b
/*-----------------------------------------------------------------*/
/* Classes before ES5
-------------------------------------------------------------------*/
// Are hoisted
const dima = new Person(1, "Dimon", 185, 75)
// console.log(dima.coef());
// Constructor function
// Includes prototype chaining (__proto__ inside the __proto__)
function Person(id, name, height, weight) {
this.id = id
this.name = name
this.height = height
this.weight = weight
// Never add methods directly to constructor functions
this.coef = function () {
let cf = this.weight / this.height
return Math.round(cf * 100) / 100
}
}
Person.prototype.inverseCoef = function () {
let inverseCf = this.height / this.weight
return Math.round(inverseCf * 100) / 100
}
// console.log(dima.inverseCoef());
// console.log(dima.constructor); // Person
/*-----------------------------------------------------------------*/
/* ES5 Classes
-------------------------------------------------------------------*/
// https://medium.com/@luke_smaki/javascript-es6-classes-8a34b0a6720a
// Really the same as constructor functions (they operate the same), just with easier lexical cover
class Footballer {
constructor(information, name, club, number){
this.name = name
this.club = club
this.number = number
}
// Will be inherited by class instances (in the .prototype)
player(){
console.log(`${this.name} plays for ${this.club} and has number ${this.number}.`);
}
getThis(){
console.log(this);
}
// Will NOT be inherited by class instances
static hey() {
console.log("Hey!!!");
}
}
const ibra = new Footballer("Ibra", "Milan", 9)
// ibra.getThis()
class Status extends Footballer{
constructor(name, club, number, status){
super(name, club, number)
this.status = status
}
getStatus(){
console.log(this.status);
}
}
const ronaldo = new Status("Ronaldo", "MU", 7, "captain")
// ronaldo.getThis()
// Testing 'this' and static methods (bound to the class, not the object)
class Me{
constructor(name, tasks){
this.name = name
this.tasks = tasks
}
logTasks(){
this.tasks.forEach(function(task){
console.log(`${this.name} has to ${task}`);
}.bind(this))
}
static staticMethod(){
console.log("I am so static.");
}
}
const dimonchik = new Me("Dima", ["exercise", "learn JS"])
Me.staticMethod();
Math.abs(1.2);
// dimonchik.logTasks()
// dimonchik.staticMethod() // Error!
// Me.staticMethod() // I am so static.