I have a Foo and a Bar class:
class Foo {
private name: string;
private bar: Bar;
constructor(name: string, bar: Bar) {
this.name = name;
this.bar = bar;
}
}
class Bar {
private x: number;
private y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
Now using the Typescript constructor shorthand I could write Foo as:
class Foo {
constructor(private name: string, private bar: Bar) { }
}
However what I would like to do, is instead of passing a Bar object into the Foo constructor. Is instead pass in the values of x and y, but still maintain using the shorthand notation to save me writing out the class in full.
Effectively, can this:
class Foo {
private name: string;
private bar: Bar;
constructor(name: string, x: number, y: number) {
this.name = name;
this.bar = new Bar(x, y);
}
}
Be written making use of the shorthand notation?
xandyvalues as separate parameters instead of aBarobject? Is it to encapsulate theBarcreation inside theFooconstructor, rather than creating one in calling code?