is it possible to have more than one constructors for a class in javascript? i.e. one with zero parameters, one with one, one with two, etc...
if so, how?
thanks!
is it possible to have more than one constructors for a class in javascript? i.e. one with zero parameters, one with one, one with two, etc...
if so, how?
thanks!
No, Javascript does not support function overloading.
However, inside every function you have access to an
arguments
object, which holds all the arguments supplied to the function, declared or not. You can simply look at it and decide what exactly you want to do in your constructor.Bad, unrefined example: