Last active
October 17, 2019 11:22
-
-
Save rauschma/c00b747df893b8afa30bbdd0bb6357a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point { | |
constructor(...args) { | |
switch (args.length) { | |
case 2: { | |
// Normal constructor | |
const [x, y] = args; | |
this.x = x; | |
this.y = y; | |
break; | |
} | |
case 1: { | |
// Copy constructor | |
const [other] = args; | |
this.x = other.x; | |
this.y = other.y; | |
break; | |
} | |
default: | |
throw new Error(); | |
} | |
} | |
} |
@ronkorving That’s definitely better. Thanks!
Until:
class Point {
constructor(x, y) {
this.x = x * 2;
this.y = y;
}
static copy(other) {
return new Point(other.x, other.y); // now the copy's x will be double other's x
}
}
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
clone() {
return new Point(this.x, this.y);
}
static clone(point) {
return new Point(point.x, point.y);
}
}
class Point3D extends Point {
constructor(x, y, z) {
super(x, y);
this.z = z;
}
clone() {
return new Point3D(this.x, this.y, this.z);
}
static clone(point3d) {
return new Point3D(point3d.x, point3d.y, point3d.z);
}
}
What I don't know is which one is better: the instance approach ( clone()
) or the class approach (static clone(point3d)
). Although I prefer the instance approach, it's more concise, both in the definition and in the call, and the instance is always necessary.
both are valid and you could even provide both
The static variant has the advantage that it plays nice with other scenarios:
const foo = Point.fromOther(otherPoint) ;// aka copy
const foo = Point.fromLineStart(line); // conversion
const foo = Point.fromMidpoint([...geometries]);
...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Always side with readability? :)
To undermine my own example however: if the constructor does any postprocessing on the input (eg:
point.x
not being equal to thex
that goes into the constructor), my example starts to hurt.