3

I am new to learning objects and classes in Javascript. I was just wondering, why would you attach a static method to a class, like so:

class MyClass {
  static myFunction(){
    console.log('foo');
  }
}

When you can just declare a regular, custom function outside of the class like one usually does?

function myFunction() {
  console.log('foo');
}
2
  • MyClass.myFunction vs new MyClass().myFunction. If you do not need an instance of the class for the logic in the method, why require one? Though the OP is comparing a class method to a normal method. Which also involves the whole concept of OO programming, and code organization. Commented May 14, 2020 at 19:23
  • 2
    @Taplar read the question again. Commented May 14, 2020 at 19:23

3 Answers 3

4

A static "method" is just a regular function that is attached to a class. This is useful when it belongs to that class semantically, and in extreme cases necessary in an inheritance hierarchy. The class name provides a visual namespace for accessing the function, e.g. Map.from does something different than Set.from.

However, you would only ever do that when you already have an existing class. You would never create an empty class only to put a static method inside it. In such a case, a simple object literal with a regular object method suffices:

const MyObject = {
  myFunction() {
    console.log('foo');
  },
};
MyObject.myFunction();
Sign up to request clarification or add additional context in comments.

2 Comments

If someone needs to restrict the number of object instantiations like let obj=new fn() .. will it be necessary to create a static method in a class or a static function would suffice?
@LFC both would work. You could even put the restriction in the constructor of the class.
1

Mostly for namespacing. Imagine you need 10 functions of related feature. Instead of taking up 10 names from global scope, using static methods you use just one global name, being the class name.

Remember that apps often end up being complex, using 3rd party libraries. Name clashes is a real problem when complexity comes to play.

3 Comments

You also get access to class level static properties
@GetOffMyLawn that's no different with global functions. In JS, static properties are always public. That's gonna change when private properties and methods are introduced.
static properties don't use names in the global scope is what I was getting at
1

static methods are useful when you use private members. A static method will have access to the members whereas a normal function won't. In the example below the static sum method, can read the private #value member of the class MyMember. This allow the sum to be calculated.

class MyNumber {
    #value = 2;
    constructor(value) { this.#value = value }
    print(title) { console.log(title, this.#value) }
    static sum(a,b){
      return new MyNumber(a.#value + b.#value); 
    }
}
let one = new MyNumber(1);
let two = new MyNumber(2);
let onePlusTwo = MyNumber.sum(one, two);
onePlusTwo.print('onePlusTwo')

This code will output

onePlusTwo 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.