Both static methods and prototype methods exist independent from any instances. The difference is that a prototype method expects to be called on an instance, i.e. to have an instance passed as the this
argument, whereas the static method does not require an instance and expects none.
Even without placing them anywhere on the class, we can see this distinction in the following example:
function myStaticMethod(arg) {
console.log('Doing something with '+arg);
}
function myMethod(arg) {
console.log('Doing something with '+this+' and '+arg);
}
const myInstance = new MyClass();
myStaticMethod('value');
myMethod.call(myInstance, 'value');
Now the .call()
syntax is not very ergonomic, we prefer myInstance.myMethod('value')
, so that is why we place the method on the prototype object of the class, having it inherited by all instances.
For static methods, this is not necessary. They don't need an instance, we don't want to call them on an instance, we want to call them as MyClass.myStaticMethod('value')
so that is where we place them. We could put them on the prototype as well, but that would lead to confusion (myInstance.myStaticMethod()
), name collisions, and unncessarily long invocations (MyClass.prototype.myStaticMethod()
). It's imaginable to write new MyClass().myStaticMethod()
, but there you would unnecessarily create an instance that is not required (and it might not even be possible to create).