Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Data {
- public get values(): Array<number> {
- // Return a defensive copy of the array
- return Object.assign([], this._values);
- }
- private readonly _values: Array<number>;
- constructor(size: number) {
- this._values = Array(size);
- }
- private setValueAtIndex(index: number, value) {
- if (index < 0 || index + 1 > this._values.length) {
- throw new Error('Invalid index');
- } else if (value < 0) {
- throw new Error('Value should be positive');
- }
- }
- }
- const data = new Data(3);
- const values = data.values;
- values[0] = -10;
- console.log(`array inside data object: ${data.values}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement