Learn JavaScript STATIC keyword in 8 minutes! ⚡

preview_player
Показать описание
// 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;
}
}

// ------------ EXAMPLE 2 ------------

class User{

static userCount = 0;

constructor(username){
}

static getUserCount(){
}
sayHello(){
}
}

const user1 = new User("Spongebob");
const user2 = new User("Patrick");
const user3 = new User("Sandy");

Рекомендации по теме
Комментарии
Автор

// 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);




// 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();

BroCodez
Автор

Seriously, you made this concept so easy to understand. If only MDN was able to be this concise. You need a degree in computer science to understand that page.

jordanaktiga
Автор

thank you so much! your tutorials are short and concise. great explanations for beginners, but short and sweet--suitable for someone switching from another language!

williamhu
Автор

So it started to make sense in the second example. I understood the first I just didn't know why you would actually want that. The second one makes sense, some small edge cases but useful when you need it.

Is this not possible with classic object constructors? for instance a way to track how many objects a constructor has made, or a factory, for that matter. I imagine it's pretty much exactly the same in a factory.

Panjax
Автор

thank you for the clear and brief explanation

xTRellyx
Автор

<*_> This is my seal. I have watched the entire video, understood it, and I can explain it in my own words, thus I have gained knowledge. This is my seal. <_*>

piotrmazgaj
Автор

Let us all appreciate the fact that he used legendary SpongeBob characters as examples 😃🙏

guycn
Автор

Class in JS, it hard subject, thanks for explanation Bro!

cmdsrxt
Автор

What if we create a User object/variable inside a scope?
When we create it, the constructor will be called increasing the userCount for the class.
But when its scope ends, this object in going to get deleted but the userCount is not updated/decremented.
```
const user1 = new User("Spongebob"); // userCount <- 1
if (true) {
const user2 = new User("Patrick"); // userCount <- 2
}

const user3 = new User("Sandy"); // userCount <- 3

// user2 is not available here, only user1 and user3 are
console.log(userCount); // prints "3"
```

professorpoke
Автор

end of day 4
WAITING FOR FUTURE VIDEOS❤

Gigglesaintlame
Автор

good except some unnecessary confusions

tonytodd
Автор

class Person {
static name = 'Ankit';
}
why im getting this error Class properties must be methods. Expected '(' but instead saw '='

AnkitSingh-ffew
Автор

hey bro i have a project using react node js and mongobd
but i dont know anything about it
do i begin by learning js then react then node js? and thank you

dazaiosamuu-yisf