'Pass values to object of extending class possible?
I'm not sure, if I'm thinking something stupid (maybe recursive): I've got 2 classes to build 2 objects with the same params, one shared method and one has 2 private methods (They are 2 polygons, one just fix as background, the other on top as progress"Arc", so needs more calculation).
For now I create the combined instance like this:
const createExergon = (radius = 100, points = 5, strokeWidth = 0, progress = 50) => {
//method calcPoints
let backgr = new PolygonBG(radius, points, strokeWidth);
//methods calcPoints(from super), length, gradient
let progr = new PolygonPG(radius, points, strokeWidth, progress);
return { backgr, progr }
};
let exergon = createExergon();
Now I wonder, whether it might be possible to execute calcPoints() only once and share the values instead of the method...
(sry, someone has 'modified' the tags, but it actually is TS)
EDIT: This is the structure I've got now (I'm new to classing in ts, so might be not the nicest way...)
class Point {
x: number;
y: number;
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
};
interface Poly {
radius: number;
points: number;
strokeWidth: number;
readonly coords: Point[];
readonly length?: number;
readonly gradient?: number[];
}
//TODO search a way to pass defValues
let defValues = { radius: 100, points: 5, strokeWidth: 0 };
// creates a fix Polygon (just as is or as bg for progressPoly)
class PolygonBG implements Poly {
radius: number;
points: number;
strokeWidth: number;
readonly coords: Point[];
constructor(radius=100, points=5,strokeWidth=0) {
this.radius = radius;
this.points = points;
this.strokeWidth = strokeWidth;
this.coords = this.calcPoints();
}
/**
*
* @returns points of the regular polygon with the given attributes
*/
calcPoints() {
let p: Point[] = []
...
};
return p;
};
};
// creates a polygon for progress calculating
class PolygonPG extends PolygonBG {
progress: number;
readonly length: number;
readonly gradient: number[];
constructor(radius = 100, points = 5, strokeWidth = 0, progress = 50) {
super(radius, points, strokeWidth);
this.length = this.len();
this.gradient = this.grad();
this.progress = progress;
};
private len(): number {
//here it needs this.coords from calcPoints()
...
return l;
};
private grad() {
let g: number[] = [];
for (let i: number = 0; i < this.points; i++) {
//here it needs this.coords from calcPoints()
...
}
return g;
};
};
//here created with def values for simplicity
const createExergon = () => {
let backgr = new PolygonBG();
let progr = new PolygonPG();
return { backgr, progr }
};
let exergon = createExergon();
so calcPoints() get executed twice (with the same params returning same result but at creating 2 different included objects), which makes sense, but isn't nice this way. I think I've got a major logic-problem in here, which I can't resolve somehow. (sorry for spamming here. I hope someone might give me the saving idea.)
Solution 1:[1]
I guess I now go with this one to pass the results of calcPoints() as values from PolygonBG to PolygonPG.coords:
TS Partial<Type>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BarbWire |
