I have a specific object that includes an instance variable holding a collection of other objects. Right now, my goal is to enhance this list of elements by adding a customized toString() method (which each Element already possesses). I experimented with the following solution and successfully executed it in the browser:
class Cell {
constructor(public id: string, public elements: Element[]) {
this.elements.toString = (): string => this.elements.join(' | ');
}
}
Despite its success in the browser, while running tests using jasmine, I encountered the error
TypeError: Attempted to assign to readonly property.
during the initiation of a Cell.
What approach would be most effective for obtaining a personalized toString() method for the list (rather than displaying them as [elem1, elem2, elem3]
)? Is there a way to reassure jasmine that this reassignment is permissible?