Advertisement
Guest User

Untitled

a guest
May 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. class Data {
  2. public get values(): Array<number> {
  3. // Return a defensive copy of the array
  4. return Object.assign([], this._values);
  5. }
  6. private readonly _values: Array<number>;
  7. constructor(size: number) {
  8. this._values = Array(size);
  9. }
  10. private setValueAtIndex(index: number, value) {
  11. if (index < 0 || index + 1 > this._values.length) {
  12. throw new Error('Invalid index');
  13. } else if (value < 0) {
  14. throw new Error('Value should be positive');
  15. }
  16. }
  17. }
  18.  
  19. const data = new Data(3);
  20. const values = data.values;
  21. values[0] = -10;
  22. console.log(`array inside data object: ${data.values}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement