Premium Only Content
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();
-
35:27
megimu32
5 hours agoMEGI + PEPPY LIVE FROM DREAMHACK!
131K10 -
1:03:23
Tactical Advisor
9 hours agoNew Gun Unboxing | Vault Room Live Stream 044
206K31 -
19:12
Robbi On The Record
10 hours ago $16.92 earnedThe Loneliness Epidemic: AN INVESTIGATION
48.3K78 -
14:45
Mrgunsngear
1 day ago $98.73 earnedFletcher Rifle Works Texas Flood 30 Caliber 3D Printed Titanium Suppressor Test & Review
81.6K21 -
17:17
Lady Decade
1 day ago $6.12 earnedMortal Kombat Legacy Kollection is Causing Outrage
54.8K9 -
35:51
Athlete & Artist Show
1 day ago $11.13 earnedIs Ryan Smith The Best Owner In The NHL?
75.8K6 -
22:56
American Thought Leaders
2 days agoCharles Murray: I Thought Religion Was Irrelevant to Me. I Was Wrong.
64.6K31 -
36:22
Brad Owen Poker
11 hours agoGIGANTIC $17,000+ Pot In BOBBY’S ROOM! TRAPPING Top Pro w/FULL HOUSE!! Big Win! Poker Vlog Ep 326
66K3 -
3:53
GreenMan Studio
1 day agoRUMBLE RUNDOWN: DREAM HACK SPECIAL W/Greenman Reports
58.3K10 -
1:28
Damon Imani
2 days agoThey Laughed at Trump’s Cognitive Test — Damon Made Them REGRET It!
54.2K37