Reputation: 3954
I have a number of artworks I want to put into an array of arrays. each artwork has properties I'm trying to describe with a class. I want to create the types with the class and push data onto the array. I'm not sure what the constructor is doing either? I think it should provide a way of creating a new instance of the class?
My ts code is as follows:
class Artwork {
private _artTitle: string;
private _slideUrl: string;
private _artPrice: string;
private _artMedium: string;
private _artDimensions: string;
private _artDesc: string;
constructor(
artTitle: string,
slideUrl: string,
artPrice: string,
artMedium: string,
artDimensions: string,
artDesc: string
){
this._artTitle = artTitle;
this._slideUrl = slideUrl;
this._artPrice = artPrice;
this._artMedium = artMedium;
this._artDimensions = artDimensions;
this._artDesc = artDesc;
}
}
let Artwks: string [];
Artwks.push(new Artwork());
Artwks[0].slideUrl = './assets/SBI_Slide_1.jpg';
Artwks[0].artTitle = "'Surprising Breakfast Ideas'";
Artwks[0].artPrice = "£400";
Artwks[0].artMedium = "Acrylic on canvas";
Artwks[0].artDimensions = '7" x 12"';
Artwks[0].artDesc = '...Inspired by trailing clauses and footling concerns, this latest injunction into the darkened forest of prevarication yields a miasma of plethoras, tantalising us with a fleeting array of nuances...';
console.log(Artwks);
Upvotes: 1
Views: 8674
Reputation: 434
The following code should work:
let artworks: Artwks[] = [];
artworks.push(new Artwork(
"'Surprising Breakfast Ideas'",
'./assets/SBI_Slide_1.jpg',
"£400",
"Acrylic on canvas",
'7" x 12"',
'...Inspired by trailing clauses and footling concerns, this latest injunction into the darkened forest of prevarication yields a miasma of plethoras, tantalising us with a fleeting array of nuances...';
));
You must fill all the parameters in the constructor to create a new instance of your class
Upvotes: 1
Reputation: 249756
You need to declare the variable and call the constructor for the class like so:
let Artwks: Artwork []=[];
Artwks.push(new Artwork("'Surprising Breakfast Ideas'", '', '', '', '', ''));
However if your class has only data I would recommend making it an interface and using json literals to create the array element :
interface Artwork {
artTitle: string;
slideUrl?: string; //optional memeber
artPrice: string;
artMedium?: string;
artDimensions?: string;
artDesc?: string
}
let Artwks: Artwork []=[];
Artwks.push({
artTitle: '',
artPrice: '',
artDesc: '',
artDimensions:''
});
This version is a lot more readable, and makes it simpler to maskes some member optional
Upvotes: 2
Reputation: 11
You can pass the parameters to the constructor to create new instance with properties you specify as follow:
You need also to initiate new empty array before you push the the Artwork object to it.
let Artwks:Array<Artwork>=[];
let artwk = new Artwork("'Surprising Breakfast Ideas'",'./assets/SBI_Slide_1.jpg', "£400", "Acrylic on canvas", '7" x 12"', '...Inspired by trailing clauses and footling concerns, this latest injunction into the darkened forest of prevarication yields a miasma of plethoras, tantalising us with a fleeting array of nuances...');
Artwks.push(artwk);
console.log(Artwks);
Upvotes: 0