In JavaScript, I wrote a class that can be optionally instantiated with the new
keyword, but can also be called statically as well. What is the best way to demonstrate this behavior in comment syntax? I was thinking something like this:
// localize constant
var PI = Math.PI;
/**
* Complex constructor
* optionally instantiate with `new`
*
* @class Complex
* @constructor
* @static
* @param r {Number} real part
* @param i {Number} imaginary part
* @param m {Number} optional, magnitude
* @param t {Number} optional, argument
* @return {Object} complex number
*/
function Complex(r, i, m, t) {
if (!(this instanceof Complex)) {
return new Complex(r, i, m, t);
}
if (typeof m === 'number') {
this.m = Math.abs(m);
} else {
this.m = Math.sqrt(r * r + i * i);
}
if (typeof t === 'number') {
this.t = (m >= 0 ? t : t + PI);
} else {
this.t = Math.atan2(i, r);
}
// limit argument between (-pi,pi]
this.t = PI - (PI * 3 - (PI - (PI * 3 - this.t) % (PI * 2))) % (PI * 2);
this.r = r;
this.i = i;
}
r, i, m, t
\$\endgroup\$