I have been working with JS for a few months now and I stumbled upon this interesting (at least to me) thing.
Take a look at this code:
class myClass
{
constructor()
{
this.x = 0;
}
}
function myFunc(){}
myClass.myFunc = myFunc;
I can call myClass.myFunc(), but myClass is just a function. In fact, I could rewrite it as
function myClass()
{
this.x = 0;
}
and it would behave the same and I would still be able to call myClass.myFunc(). So what actually is a function (like, "under the hood")? It seems to behave more like an object that can be called if it makes any sense, thus being able to have fields of its own.
Also, is this considered bad practice? In node I have
module.exports = myClass;
module.exports.myFunc = myFunc;
Is this okay to do? Or is this frowned upon? What are the cons, if any?
EDIT: I'm not asking how to have a static method, I'm asking why this happens.