Learn JavaScript STATIC keyword in 8 minutes! ⚡
ฝัง
- เผยแพร่เมื่อ 17 พ.ย. 2023
- // 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.
thank you for the clear and brief explanation
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.
Great explanation Bro ❤
thank you very helpful video
Class in JS, it hard subject , thanks for explanation Bro!
very useful
Hey man, more turorials on C?
end of day 4
WAITING FOR FUTURE VIDEOS❤
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 ;-;
pls more react content :(
I love you
good except some unnecessary confusions
hi bro
hi
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)
Hi code bro pls lua
hi bro