From what I've read in Uncle Bob's 'Clean Code' book, the following is recommended: 'When overloading constructors, use static factory methods with names that describe the arguments.' For example:
Complex fulcrumPoint = Complex.FromRealNumber(23.0);
Is better than:
Complex fulcrumPoint = new Complex(23.0);
How should the code of the Complex class be to be able to carry it out according to the first assumption? As with Java, how could it be implemented with JavaScript?
Thanks.
For the Java part it would only be necessary to do something like this:
Simply in the static method you create a new object of the Complex class and return it.
For the JavaScript part, what you propose doesn't make much sense, because in JavaScript you can't overload a function. If you define a function with the same name:
Notice if you run this, you might expect "Without Parameters" to be typed followed by "With Parameters". But that doesn't happen, and it writes "With parameters" twice because when defining a function with the same name twice, the second one overwrites the first one.
In addition, in JavaScript the parameters are not typed, the type of the parameters is not indicated in the definition of the functions, so in the event that the function could be overloaded, it would not be known which version of the function to use, since be able to determine the type of parameter. So in the end, you're going to end up needing functions with different names.
More info:
Here are some brief examples of how that class could be with comments in the code, any questions ask!
Java
javascript