8

What sort of issues with ES5 are static class methods in ES6 supposed to deal with?

The Babel documentation has the following example in its section regarding ES6 classes, though it does not actually state what this pattern accomplishes.

Classes support prototype-based inheritance, super calls, instance and static methods and constructors

class SkinnedMesh extends THREE.Mesh {
  constructor(geometry, materials) {
    super(geometry, materials);

    this.idMatrix = SkinnedMesh.defaultMatrix();
    this.bones = [];
    this.boneMatrices = [];
    //...
  }
  update(camera) {
    //...
    super.update();
  }
  static defaultMatrix() {
    return new THREE.Matrix4();
  }
}
Walter Roman
  • 4,621
  • 2
  • 32
  • 36
  • 5
    Personally, I use the `static` keyword to indicate to the developer that *this method doesn't use information from the current* instance - similar to using underscore-prefixed names to indicate private properties - even though that may not necessarily be the case, it's a nice bit of self-commenting if used correctly – CodingIntrigue Jun 15 '15 at 11:38
  • Very good example, thanks! – Martin Meeser Apr 14 '16 at 09:18
  • May I suggest to rename 'defaultMatrix' to 'createDefaultMatrix', because it is a very classical 'Factory Method' pattern (encapsulation of 'new' operator), and many agree to that convention. – Martin Meeser Apr 14 '16 at 09:29
  • @MartinMeeser As stated in the question, this code was pulled from Babel's documentation. – Walter Roman Apr 19 '16 at 19:47

2 Answers2

9

If you compile ES6 code with Babel, and some class contains a static method, ES5-generated code will be just adding that static function to the constructor function.

So, this ES6 ES2015 code:

class A {
   static doStuff() {}
}

...equals (in ES5):

function A() { }
A.doStuff = function() { };

Why you need static functions? Well, transpiled code won't support statics at all, since even functions are objects and static functions are turned into own properties of constructor function.

Static functions or properties can be used to implement factory pattern:

class A {
   static create() {
      // Specific factory method code
   } 
}

var instance = A.create();

Anyway, static member usage is a very diffuse topic and it goes out of scope of an objective answer. It has a lot of use cases and these are universal to any programming language.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
4

Consider class that contains only static methods:

class MyNamespace {
  static foo() { ... }
  static bar() { foo(); }
}

It is quite convenient way of organizing code - put stuff in namespaces.

MyNamespace.foo();
MyNamespace.bar();

That's other than standard static method use cases in other OOP languages.

Community
  • 1
  • 1
c-smile
  • 26,734
  • 7
  • 59
  • 86
  • 2
    Use object literals for namespaces. Not empty classes. – Bergi Feb 08 '16 at 16:38
  • @Bergi Object literals have very inconvenient syntax for that purpose. You have to remember to put `,` and use anonymous functions, etc. – c-smile Feb 08 '16 at 18:26
  • There's no reason to anonymous functions? You can just do `const MyNamespace = { foo() { … }, bar() { … } };` – Bergi Feb 08 '16 at 18:31
  • @Bergi Commas are mandatory in object literals. So if you have large functions you will easily get lost in sequences like `{}}},},};` and the like... Anyway it is a matter of taste, both cases are conceptually the same. – c-smile Feb 09 '16 at 02:18
  • @Bergi If not namespaces, what do you recommend to use static methods/properties for? – 1252748 Jan 11 '18 at 19:22
  • 1
    @1252748 Whenever you have functions that do not operate on an instance but definitely relate to the `class`, you can put them as static methods on the class (where the constructor acts as a namespace). However, this does not work the other way round: if you want to make a namespace but don't have instances or a constructor, do not use `class`. – Bergi Jan 11 '18 at 19:44
  • Imo: If one get lost in `{}}},},};` it's time to re-factor. (And of course always use TABS for indentation). – user3342816 Mar 29 '23 at 03:04