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();
  }
}
4
  • 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 Commented Jun 15, 2015 at 11:38
  • Very good example, thanks! Commented Apr 14, 2016 at 9: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. Commented Apr 14, 2016 at 9:29
  • @MartinMeeser As stated in the question, this code was pulled from Babel's documentation. Commented Apr 19, 2016 at 19:47

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

what is a constructor function object functions?
@1252748 An own property of constructor function. I'm going to edit.
Oh, that is a bit clearer, yes. But I am not the OP, I was just browsing the topic and stumbled upon your explanation. Thanks though :)
@1252748 You don't need to necessarily be the OP to suggest improvements :D
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.

7 Comments

Use object literals for namespaces. Not empty classes.
@Bergi Object literals have very inconvenient syntax for that purpose. You have to remember to put , and use anonymous functions, etc.
There's no reason to anonymous functions? You can just do const MyNamespace = { foo() { … }, bar() { … } };
@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.
@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.
|

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.