-1

I am a beginner in Javascript and I didn't understand the concept of static variables or methods inside Class. I don't understand why and when we need them and what is the use case for them? for example this class

class Car {
  constructor(name) {
    this.name = name;
  }
  static hello() {
    return "Hello!!";
  }
}
  • The idea is that the static method is can be called without creating a new instance of the class. That means you can call it inside of an instance's method the same way you'd call it outside of the instance. – Nilesh Mishra Mar 10 '22 at 22:47
  • 1
    Understanding static methods isn't limited to Javascript. You should study object-oriented principles carefully, as it's a basic understanding that will translate well to a variety of OO languages. You'll likely get a lot of varied answers to your question, as this is particularly opinion-based, but I'd recommend the following article: https://medium.com/att-israel/should-you-avoid-using-static-ae4b58ca1de5 – BenM Mar 10 '22 at 22:47
  • I'm going to mark this as a duplicate, but want to make sure it's understood that the ANSWERS in the link below explain what static variables are (even though the question asks "how" to create a static variable). – devlin carnate Mar 10 '22 at 22:55
  • Does this answer your question? [Static variables in JavaScript](https://stackoverflow.com/questions/1535631/static-variables-in-javascript) – devlin carnate Mar 10 '22 at 22:56
  • @NileshMishra static methods *cannot* be accessed in an instance's method. – code Mar 10 '22 at 23:06
  • @code we can access static method in an instance's method check [this](https://codepen.io/nilesh9836/pen/XWVrWjW?editors=1111). – Nilesh Mishra Mar 10 '22 at 23:18
  • @NileshMishra well, what I meant is that you can't *directly* access static methods; I was rereferring to the fact that static methods can't be accessed with the `this` pointer. Oh well, that works anyway though. – code Mar 11 '22 at 00:53

1 Answers1

0

OOP (including classes) was recently added to JavaScript, and in my opinion, it's more like syntactic sugar that allows us to structure our data more easily, or perhaps just for the satisfaction of using the new keyword. These OOP features were adapted from other languages, such as Java.

Static methods allow other pieces of your code to access variables and methods from a class without creating an instance of it (not using the new keyword).

This is especially helpful when you want to retrieve data from a class without creating an instance or initializing the constructor.

Take, for example, a real world example of Web Components:

class WhyStatic {
  /* other methods... */
  static get observedAttributes() {
    return ["name", "why", "test"];
  }
}

This allows the custom elements function to access the observed attributes of an HTML element without creating an extra instance.

In other words, we can create non-instance specifc data for a class that gets accessed outside, and only outside, a class instance.

That's why we need static in OOP.

code
  • 5,690
  • 4
  • 17
  • 39
  • @MohamedMahgoob great! If it answered your question would you kindly select it as the answer? – code Mar 11 '22 at 18:26
  • Can you please tell me how can I make it answered as I am also new to stack overflow? Thank you for your help in advance – Mohamed Mahgoob Mar 11 '22 at 23:35
  • @MohamedMahgoob simply click the [checkmark next to the vote sign.](https://i.stack.imgur.com/ULMp3.png) – code Mar 11 '22 at 23:49