Learn JavaScript STATIC keyword in 8 minutes! ⚡
// static = keyword that defines properties or methods that belong
// to a class itself rather than the objects created
// from that class (class owns anything static, not the objects)
// ----------- EXAMPLE 1 -----------
class MathUtil{
static PI = 3.14159;
static getDiameter(radius){
return radius * 2;
}
static getCircumference(radius){
return 2 * this.PI * radius;
}
static getArea(radius){
return this.PI * radius * radius;
}
}
console.log(MathUtil.PI);
console.log(MathUtil.getDiameter(10));
console.log(MathUtil.getCircumference(10));
console.log(MathUtil.getArea(10));
// ----------- EXAMPLE 2 -----------
class User{
static userCount = 0;
constructor(username){
this.username = username;
User.userCount++;
}
static getUserCount(){
console.log(`There are ${User.userCount} users online`);
}
sayHello(){
console.log(`Hello, my username is ${this.username}`);
}
}
const user1 = new User("Spongebob");
const user2 = new User("Patrick");
const user3 = new User("Sandy");
user1.sayHello();
user2.sayHello();
user3.sayHello();
User.getUserCount();
-
2:24:44
vivafrei
22 hours agoEp. 211: Trump Trial, Jack Smith, Tyson Loses, Ukraine, Russia V. Google & MORE!
154K322 -
3:02:10
Due Dissidence
21 hours agoSeinfeld Show INTERRUPTED by Protests, Police CRACK DOWN on Nakba March, Cardi B TRASHES Biden
94.6K65 -
15:48
PARAGRAPHIC
21 hours agoThe Largest Reptile Sanctuary in the United States | PARAGRAPHIC
42.3K19 -
27:45
Adam Does Movies
17 hours ago'The Strangers: Chapter 1' Is Hot Garbage - SPOILERS!
42.2K5 -
8:33
MichaelBisping
1 day agoBISPING Reacts: "Tyson Fury COULDN'T Keep The Pace!" | Tyson Fury vs Oleksandr Usyk Reaction
51.2K18 -
9:56
Misha Petrov
17 hours agoTragic Leftist Transformations
61.1K57 -
31:48
CarlCrusher
17 hours agoCurse of the Skinwalker | The Ancient Mystery of Magic Mesa - Full Documentary | Part 2
57.4K22 -
1:38
AnthiBozoviti
1 day agoImprovisation on Ancient Greek Lyre | Anthi Bozoviti
43.4K26 -
LIVE
World Poker Tour
27 days agoWPT Live!
84 watching -
25:30
GEN
2 days ago $0.03 earnedWhat Happens When a City Decriminalizes All Drugs?
56.2K149