Learn JavaScript STATIC keyword in 8 minutes! ⚡
HTML-код
- Опубликовано: 2 окт 2024
- // 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();
// 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();
hey bro good content👍👍can you make a tutorial about jdbc
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.
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.
thank you for the clear and brief explanation
Let us all appreciate the fact that he used legendary SpongeBob characters as examples 😃🙏
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
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.
Finally understood the “this” keyword as well as the static behavior. Thank you!
very useful
class Person {
static name = 'Ankit';
}
why im getting this error Class properties must be methods. Expected '(' but instead saw '='
I tried your code and it works.
Make sure you're trying to access name on the Person class itself, like so: console.log(Person.name)
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!
Hey man, more turorials on C?
hi
Class in JS, it hard subject , thanks for explanation Bro!
end of day 4
WAITING FOR FUTURE VIDEOS❤
thank you very helpful video
I love you
good except some unnecessary confusions
pls more react content :(
Great explanation Bro ❤
hi bro
hi bro
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
You need to know the core concepts of js if you want to move to react & node. React is a JS framework and node is JS backend
Same situation here, in my college. I'm literally speedrunning the entire course because there are so many things to learn and I dont know even the language to start with. 3 months to end the project ;-;
Hi code bro pls lua