Let’s say I have a playlist class like this one:
class playlist{
constructor(urls){
this.tracks=urls;
}
}
I want to use a javascript setter to create an array of track objects based on the urls input.
class track{
constructor(url,num){
this.url=url;
this.num = num;
}
}
class playlist{
constructor(urls){
this.tracks=urls;
}
//tracks setter
set tracks(urls){
this.tracks = urls.map(function(url,index) {
return new track(url,index+1);
})
}
}
This is fine.
But if I remove some items of the tracks array of the playlist object, if I slice it, etc; the num properties of the track objects will not « update » : I want their num property to be always the track index+1.
Is it possible to automate this and how?
EDIT
The idea behind those classes is to extend standardized JSPF objects, in the goal of having {...playlistdata} or {...trackdata} output a regular JSPF object - the code above has been simplified for the question.
Thanks!