Node JS, Static Function / Variable Best Practice

11,022

As I am new to Node JS world, I want to make sure that this is right way to implement class with static and non static methods.

First of all, keep in mind that there are no classes in JavaScript - even in ES2015 (ES6) and beyond. JavaScript uses prototypal inheritance and the class keyword is just a syntax sugar for prototypes, not classes. There are also no private and public properties in the sense that Java implements them using a static type system.

In JavaScript to have a truly private variable you use closures like this:

let f = (x => () => x++)(0);

Here the f() function has a truly private variable that no one else has access to and it is shared among all invocations of f() so every time it is called it increments the counter but you have no way to access the counter in any other way than using the f() function.

Keeping in mind that there are no classes in JavaScript but a newer form of OO-inheritance called prototypes (which first appeared in the Self language), there is a misleadingly named class keyword that lets you define prototypes more easily using syntax like:

class Polygon {
  constructor(height, width) {
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }
  static distance(a, b) {
    const dx = a.x - b.x;
    const dy = a.y - b.y;

    return Math.sqrt(dx*dx + dy*dy);
  }
}

class Square extends Polygon {
  constructor(length) {
    super(length, length);
    this.name = 'Square';
  }
}

See:

My advice would be to pretend that you don't know Java when you learn the object oriented nature of JavaScript because they may look similar but they are actually very different.

Actually JavaScript is more like Scheme with Java-like syntax, Self-style OOP plus event loop based asynchronous, nonblocking I/O. It's literally nothing like Java. And by the way, unlike Java it has real lambdas that implement lexical closures so you also need to keep that in mind.

Share:
11,022
codereviewanskquestions
Author by

codereviewanskquestions

Updated on June 04, 2022

Comments

  • codereviewanskquestions
    codereviewanskquestions almost 2 years

    I have a Java background, I am trying to build a Node JS lib. I have a class in Java like this;

    public class MyClass {
        static List<String> myList = initMyList();
        private String something;
    
        public MyClass(String something) {
            validate(something);
            this.something = something;    
        }
    
        public static String doSomething(String something) {
            return doSomethingWithMyList(something);
        }
    
        public String doSomething() {
            return doSomething(something);
        }
    
        private static String doSomethingWithMyList (String something) {
            // do something with list ..
            return something
        }
    }
    

    As you can see, it has a static helper method static String doSomething takes String param and non static function String doSomething uses the instance variable, something.

    So users can do either MyClass.doSomething(string) or MyClass m = new MyClass(sting); m.doSomething(). The former wouldn't do validation but the later will do the validation in the constructor.

    I want to do this in Node JS. And I have this.

    // construction function
    function MyClass(something) {
      validate(something);
      this.something = something;
    }
    
    // static variable 
    MyClass.myList = someModule.getList();
    
    // static function
    MyClass.doSomething = function doSomething (something) {
      return something;
    };
    
    // private static
    MyClass._doSeomthingWithMyList = function _doSeomthingWithMyList (something) {
      // do something wiht list
      return something;
    };
    
    
    MyClass.prototype.doSomething = function doSomething() {
      MyClass.doSomething(this.something);
    };
    
    module.export = MyClass;
    

    As I am new to Node JS world, I want to make sure that this is right way to implement class with static and non static methods.