What are the benefits of using classes in js compared to creating standalone functions


duxfox--

I do a lot of full stack JS work and when creating files with encapsulated logic I usually follow this approach:

export const SOME_KEY_TO_STATE = 'some-key';
export const ANOTHER_KEY_TO_STATE = 'another-key';

let moduleState = {};

export function modifyState(someArg) {
  // ... do some logic
  // ... perhaps derive some new value based off of logic
  const newValue = derivedNewValue;
  moduleState[someKey] = newValue;
}

export function getSomeState(aKey) {
  // ... do some sanity checking?
  const initialValue = moduleState[aKey];

  // ... calculate value based on some conditions?
  const finalValue = calculatedValue;
  return moduleState
}

I've also used classes occasionally, which actually provide the same structure, except that the module state will be inside the class as instance variables, and possibly static variables for those exported constants:

export default class SomeThing {
  static SOME_KEY = '';

  state = {};

  modifyState(arg) { ... }
  getSomeState() { ... }
}

My preferred method is the first, mainly because I can just import what I need in the rest of the code without having to float the whole object (and its state and other methods I might not use - or am I leaving it entirely?) . Also, if I want to reference the function in the context, I can alwaysimport { * as someName } from myModule

However, I'm curious if there are benefits to using classes instead of the first approach I outlined?

Begui
let moduleState = {};

This is global state and is best avoided . While it can be nicely encapsulated in a module, it's a static variable and essentially a singleton - it only exists once in the entire application, created when the module is loaded.

In some cases this may be appropriate, but generally don't use singletons !

A classsolution has the obvious advantage that you can instantiate it as many times as you want, anywhere you want (like a test). So make sure you understand this difference and choose the appropriate mode.

I like the first one mainly because I can just import what I need in other parts of the code without having to float the whole object (and its state and other methods that I might not use)

Indeed it is. You always have full module objects, states and functions floating around in your application, that doesn't mean they're optimized. Of course, if you don't importspecify them explicitly, they're out of your scope, but classit doesn't really hurt to have an instance available. If you don't call all its methods, it's like not importing all functions from the module.

The syntax difference between calling a normal function or calling a method on an object is really just a minor detail and shouldn't affect your decision. You can always get around it by namespace importing the module or destroying the object.

Related


What benefits do I get from JSVC compared to just using systemd?

User636044: The Tomcat documentation describes the process of compiling and installing JSVC , which can be used to run Tomcat as a daemon. From what I understand, JSVC has two benefits: It starts as root, allowing privileged ports (eg 80 or 443). 它创建一个“控制器进程”,

What benefits do I get from JSVC compared to just using systemd?

User636044: The Tomcat documentation describes the process of compiling and installing JSVC , which can be used to run Tomcat as a daemon. From what I understand, JSVC has two benefits: It starts as root, allowing privileged ports (eg 80 or 443). It creates a

What benefits do I get from JSVC compared to just using systemd?

User636044: The Tomcat documentation describes the process of compiling and installing JSVC , which can be used to run Tomcat as a daemon. From what I understand, JSVC has two benefits: It starts as root, allowing privileged ports (eg 80 or 443). It creates a

What benefits do I get from JSVC compared to just using systemd?

username The Tomcat documentation describes the process of compiling and installing JSVC , which can be used to run Tomcat as a daemon. From what I understand, JSVC has two benefits: It starts as root, allowing privileged ports (eg 80 or 443). It creates a "co