There is one more related case, although in JS it can only define functions:
Class Human {
d() {} // Property d, its type is Function.
}
Basically your c
is very different from the other three. You can only access your c
as Human.c
, while all the others can be accessed as properties of an instance of the Human
class, e.g. new Human().a
.
To define methods, the preferable syntax is probably like d
, it looks most natural. It's effectively the same as if you assigned the function to Human.prototype
, like b
. So the above example is equivalent to Human.prototype.d = function() {}
.
For instance variables (fields), the syntax for a
is probably best. Every instance of the class receives its own a
. If it was initialised to []
instead of 0
, each of them would have a separate array, which is most probably what you expect.
On the other hand, properties assigned to class prototype like your b
are visible via instances (so new Human().b
works), but it's a single object that all existing instances of your class can access - which means they should be treated as immutable, unless you like bad designs, global mutable state etc. So this is a place where you can basically put:
- functions - but for functions the better syntax is the method syntax (my
d
),
- immutable values - but then you can as well define them as a constant outside of the class, or make them static (like your
c
),
- shared state - which it might be better not to have at all.
So I would not use Class.prototype
directly at all, instead define methods as methods, and fields in the constructor.