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

Комментарии • 31

  • @BroCodez
    @BroCodez  11 месяцев назад +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 10 месяцев назад

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

  • @jordanaktiga
    @jordanaktiga 7 месяцев назад +16

    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.

  • @Panjax
    @Panjax 5 месяцев назад +3

    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.

  • @0xTRellyx0
    @0xTRellyx0 6 месяцев назад +1

    thank you for the clear and brief explanation

  • @guycn1
    @guycn1 Месяц назад

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

  • @professorpoke
    @professorpoke Месяц назад

    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

  • @piotrmazgaj
    @piotrmazgaj Месяц назад

    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.

  • @Alite4daday
    @Alite4daday 28 дней назад

    Finally understood the “this” keyword as well as the static behavior. Thank you!

  • @ariansarafraz2829
    @ariansarafraz2829 3 месяца назад

    very useful

  • @AnkitSingh-ff6ew
    @AnkitSingh-ff6ew 5 месяцев назад

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

    • @Reita-ef9pcta
      @Reita-ef9pcta 4 месяца назад

      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)

  • @williamhu9567
    @williamhu9567 Месяц назад

    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!

  • @Dividendido
    @Dividendido 10 месяцев назад

    Hey man, more turorials on C?

  • @idontknowwhattonamethischa4592
    @idontknowwhattonamethischa4592 10 месяцев назад +1

    hi

  • @ФёдорСёмочкин
    @ФёдорСёмочкин 5 месяцев назад

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

  • @Gigglesaintlame
    @Gigglesaintlame 11 месяцев назад

    end of day 4
    WAITING FOR FUTURE VIDEOS❤

  • @wassimmr2502
    @wassimmr2502 3 месяца назад

    thank you very helpful video

  • @ligiafedericci6608
    @ligiafedericci6608 5 месяцев назад

    I love you

  • @tonytodd7011
    @tonytodd7011 9 месяцев назад

    good except some unnecessary confusions

  • @nemesisxl6736
    @nemesisxl6736 10 месяцев назад

    pls more react content :(

  • @LuisPerez-sy3ly
    @LuisPerez-sy3ly 9 месяцев назад

    Great explanation Bro ❤

  • @anuptimalsina1420
    @anuptimalsina1420 10 месяцев назад

    hi bro

  • @tomeytf
    @tomeytf 11 месяцев назад

    hi bro

  • @dazaiosamuu-yi7sf
    @dazaiosamuu-yi7sf 10 месяцев назад

    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 8 месяцев назад +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 6 месяцев назад +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 ;-;

  • @Fantrap
    @Fantrap 10 месяцев назад

    Hi code bro pls lua