Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript class Instance vs Static

Tags:

javascript

There are already a few Q&A regarding the class OOP is various languages.

It appears that static method is marginally faster than Instance method but the difference is insignificant in common practical usage.
ref:
Performance of using static methods vs instantiating the class containing the methods
Speed Test: Static vs Instance Methods

  • In class instance example, 2 objects are created, the original class and a clone also using new.
    (If there is a situations where multiple processes with different data need to set values and use the same class at the same time, then creating a new clone for each process would maintain data integrity.)

  • In static example, only one object is created.

Are there any benefits to consider when deciding between the two types?

For example:

// class instance
class Triple {
  
  do(n = 1) {
    return n * 3;
  }
}
const triple = new Triple();
triple.do(5); // 15



// static method
class Triple {
  
  static do(n = 1) {
    return n * 3;
  }
}
Triple.do(5); // 15

Update constructor issue
It seems that the constructor only works in class instance.
ref:
How do I use a static variable in ES6 class?

Example:

// class instance
class TriplePlus {

  constructor() {
    this.a = 10;
  }
  
  do(n = 1) {
    return (n * 3) + this.a;
  }
}
const triplePlus = new TriplePlus();
triplePlus.do(5); // 25



// static method
class TriplePlus {

  constructor() {
    this.a = 10;
  }
  
  static do(n = 1) {
    return (n * 3) + this.a;
  }
}
TriplePlus.do(5); // NaN

Update for clarification (re comments)
Please note that above are simplified concept examples. The actual class has 10s of methods. A typical example for the static in class is a set of utility methods which perform better as OOP in a single object vs multiple individual factory functions. The focus of the topic is the comparison between class Instance vs Static. The discussion about factory function alternatives is irrelevant to this topic.

static

The static keyword defines a static method for a class. Static methods aren't called on instances of the class. Instead, they're called on the class itself. These are often utility functions, such as functions to create or clone objects.

like image 411
erosman Avatar asked Aug 17 '20 17:08

erosman


People also ask

What is the difference between static and instance methods in JavaScript?

Static methods belong to a class and don't act on its instances. This means that they can't be called on instances of the class. Instead, they're called on the class itself.

What is difference between static and instance class?

Static methods can be called without the object of the class. Instance methods require an object of the class. Static methods are associated with the class. Instance methods are associated with the objects.

What is the difference between static and instance?

Instance variables are created when an object is created with the use of the keyword 'new' and destroyed when the object is destroyed. Static variables are created when the program starts and destroyed when the program stops. Instance variables can be accessed directly by calling the variable name inside the class.

What is an instance method in JavaScript?

An instance is an object containing data and behavior described by the class. The new operator instantiates the class in JavaScript: instance = new Class() . For example, you can instantiate the User class using the new operator: const myUser = new User(); new User() creates an instance of the User class.


1 Answers

  1. Regarding first example:
// class instance
class Triple {

If you have no shared state (shared between methods) it is better to avoid a declaration of class. Just introduce several functions grouped by module. In other words, no OOP is needed if Function abstraction is enough.

  1. Second example
// static method
class Triple {

Here we have no state and just can not use this keyword inside static methods. In such a case class is just a namespace. Think twice. Do you really need explicit namespace (class) abstraction over a module (import/export)?

  1. Update constructor issue.

As I said before static parts can not reference non-static parts. And usage of this keyword is invalid. All non-static methods implicitly accept class instance as an argument. I mean

obj.method(arg1, arg2, arg3) is equivalent of method(obj as this, arg1, arg2. arg3)

Just to recap. The majority of cases do not need OOP and patterns. Functions and modules are more straightforward and maintainable.

like image 164
Alexander Alexandrov Avatar answered Oct 12 '22 23:10

Alexander Alexandrov