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

ความคิดเห็น • 25

  • @BroCodez
    @BroCodez  9 หลายเดือนก่อน +4

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

    • @mutabazibertrand3577
      @mutabazibertrand3577 8 หลายเดือนก่อน

      hey bro good content👍👍can you make a tutorial about jdbc

  • @jordanaktiga
    @jordanaktiga 5 หลายเดือนก่อน +8

    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.

  • @0xTRellyx0
    @0xTRellyx0 4 หลายเดือนก่อน +1

    thank you for the clear and brief explanation

  • @Panjax
    @Panjax 3 หลายเดือนก่อน +2

    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.

  • @LuisPerez-sy3ly
    @LuisPerez-sy3ly 6 หลายเดือนก่อน

    Great explanation Bro ❤

  • @wassimmr2502
    @wassimmr2502 หลายเดือนก่อน

    thank you very helpful video

  • @user-cm8ds7rx6t
    @user-cm8ds7rx6t 2 หลายเดือนก่อน

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

  • @ariansarafraz2829
    @ariansarafraz2829 22 วันที่ผ่านมา

    very useful

  • @Dividendido
    @Dividendido 8 หลายเดือนก่อน

    Hey man, more turorials on C?

  • @Gigglesaintlame
    @Gigglesaintlame 9 หลายเดือนก่อน

    end of day 4
    WAITING FOR FUTURE VIDEOS❤

  • @dazaiosamuu-yi7sf
    @dazaiosamuu-yi7sf 8 หลายเดือนก่อน

    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

    • @drew7272
      @drew7272 5 หลายเดือนก่อน +1

      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

    • @RafaelSilva-rv6kt
      @RafaelSilva-rv6kt 4 หลายเดือนก่อน +1

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

  • @nemesisxl6736
    @nemesisxl6736 8 หลายเดือนก่อน

    pls more react content :(

  • @ligiafedericci6608
    @ligiafedericci6608 3 หลายเดือนก่อน

    I love you

  • @tonytodd7011
    @tonytodd7011 6 หลายเดือนก่อน

    good except some unnecessary confusions

  • @tomeytf
    @tomeytf 9 หลายเดือนก่อน

    hi bro

  • @idontknowwhattonamethischa4592
    @idontknowwhattonamethischa4592 8 หลายเดือนก่อน +1

    hi

  • @AnkitSingh-ff6ew
    @AnkitSingh-ff6ew 3 หลายเดือนก่อน

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

    • @Reita-ef9pcta
      @Reita-ef9pcta 2 หลายเดือนก่อน

      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)

  • @Fantrap
    @Fantrap 8 หลายเดือนก่อน

    Hi code bro pls lua

  • @anuptimalsina1420
    @anuptimalsina1420 8 หลายเดือนก่อน

    hi bro