-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStatic.js
More file actions
100 lines (69 loc) · 2.17 KB
/
Static.js
File metadata and controls
100 lines (69 loc) · 2.17 KB
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
//: Static properties and methods
/* We can also assign a method to the class as a whole. Such methods are called static */
// In class declaration, they are prepended by static keyword
class User {
static staticMethod() {
console.log(this === User);
}
}
User.staticMethod(); // true
//? That actuallu does the same as assigning it as a property directly
class User2 {}
User2.staticProperty = function () {
console.log(this === User2);
};
User2.staticProperty(); // true
/* Usually, static methods are used to implement functions that belong to the class as a whole, but not to any particular object of it. */
// For instance, we have Article objects and need a function to compare them.
class Article {
constructor(title, date) {
this.title = title;
this.date = date;
}
static compare(articleA, articleB) {
return articleA.date - articleB.date;
}
}
// usage
let articles = [
new Article("HTML", new Date(2019, 1, 1)),
new Article("CSS", new Date(2019, 0, 1)),
new Article("JavaScript", new Date(2019, 11, 1)),
];
articles.sort(Article.compare);
alert(articles[0].title); // CSS
/* Satic method are also used in database-related classes to serch/save/remove entries from the database */
//! Static method aren't available for individual objects
/* Static methods are callable on clasees, not on individual objects */
//* Static propeties
class Person {
static name = "Ankt Yadav";
}
console.log(Person.name); // Ankit Yadav
// That is the same as a direct assignment to Person
Person.name = "Ankit Yadav";
//* Inheritance of static properties and methods
class Animal {
static planet = "Earth";
constructor(name, speed) {
this.speed = speed;
this.name = name;
}
run(speed = 0) {
this.speed += speed;
console.log(`${this.name} runs with speed ${this.speed}`);
}
static compare(a, b) {
return a.speed - b.speed;
}
}
// inherit from Animal
class Rabbit extends Animal {
hide() {
console.log(`${this.name} hides!`);
}
}
let rabbit = [new Rabbit("White Rabbit", 10), new Rabbit("White Rabbit", 5)];
rabbit.sort(Rabbit.compare);
rabbit[0].run(); // Black Rabbit runs with speed 5
console.log(Rabbit.planet); // Earth