0

When implementing a method used only within a specific component class, it can be declared as a static method and an instance method, and I wonder the difference between the two.

Declaring methods of classes used across applications, such as the Util class, static is common, but I wonder if it is the right way to declare static just because it does not refer to this within a particular component class.

According to my research, the static method of class is disadvantageous in terms of memory because the memory remains intact even if the component's life cycle ends. If so, isn't it more advantageous to use it as an instance method even if it's only used within a specific class and not referenced to this?


class MyComponent {

// ex1) instance method 
private isValidInput(text) {
  return !!text.trim()
}

// ex1) static method 
private static isValidInput(text) {
  return !!text.trim()
}

}

When implementing component classes using Angular, I wondered if I would implement util functions that are only used in specific component classes as static or instance methods.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
sjfklsaf
  • 1
  • 1
  • 1
    Utility methods that don't need the class' members shouldn't really be even a static method. Why not put them in a regular function? – kelsny Oct 31 '22 at 14:09
  • Question about the util function that only class members use within a particular class of the application @caTS – sjfklsaf Oct 31 '22 at 14:17
  • "*in the Util class, static is common*" - you should not have a `class Util` unless you want to create instances of it. You don't. You [should not have a `class` consisting of only `static` methods at all](https://stackoverflow.com/q/29893591/1048572), write plain functions instead. JS is not Java where everything needs to go inside a `class`. – Bergi Oct 31 '22 at 15:22
  • "*According to my research, the static method of class is disadvantageous in terms of memory because the memory remains intact even if the component's life cycle ends.*" - no, that's wrong. Where did you get that from? – Bergi Oct 31 '22 at 15:22
  • 1
    I am not sure how to answer questions about garbage collection, which seems like something specific to a particular runtime engine. I mean, if the class constructor is in scope somewhere, then its static methods *must not* be collected, but once the class falls out of scope the runtime can dispose of the static methods whenever it wants to, or hold onto it forever, etc. But note that what you're calling an "instance method" is also going to stay around until the class constructor goes out of scope, because it's defined on the `prototype` property of the constructor. – jcalz Oct 31 '22 at 18:02
  • So how can we answer this question authoritatively? Could you [edit] the question to clarify exactly what you're asking? (If it's about when garbage collection happens, then you should specify a runtime engine and version; if it's about methods and scope, then you should give examples that differ in terms of scope, since `Class.staticMethod` and `Class.prototype.instanceMethod` are both in scope whenever `Class` is, etc). If you do edit with an answerable version and want me to take another look, please mention @jcalz in a comment to notify me. Good luck! – jcalz Oct 31 '22 at 18:05

0 Answers0