SHARE
TWEET

Untitled

a guest May 26th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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}`);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top