title: Angular2 - read-only properties
body: <p>In OOP I was taught that your objects should only expose their members via getters and should never allow direct mutation.</p>
  
  <p>Looking at some Angular2 apps I always see this concept violated.</p>
  
  <pre><code>export class TodoService {
    todos: Array&lt;TodoData&gt; = [];
  
    addTodo(todo: string) {
      this.todos.push({
        text: todo
      });
    }
  }
  </code></pre>
  
  <p>In the code above there's no guarantee that a todo won't be added in ways other than the <code>addTodo</code> method (e.g. calling <code>todoService.todos.push({text: ''})</code> or even by overwriting the whole todos array).</p>
  
  <p>I always try to eliminate these violation possibilities, which makes my services end up looking similar to this:</p>
  
  <pre><code>export class MyService {
    private _myPrimitive: boolean;
    get myPrimitive(): { return this._myPrimitive; }
  
    private _myArray: Array&lt;number&gt;;
    get myArray(): Array&lt;number&gt; { return this._myArray.splice(0); }
  
    private _myObject: MyObject;
    getMyObject(): MyObject { return JSON.parse(JSON.stringify(this._myObject)); }
  
    private _onMyEvent = new Subject&lt;Event&gt;();
    get onMyEvent: Observable&lt;Event&gt; { return this._onMyEvent.asObservable(); }
  }
  </code></pre>
  
  <p>I don't see this style of coding in any Angular2 applications. It's not just ugly, it's also slow when it comes to change detection (firing <code>JSON.parse(JSON.stringify(object)</code> after every 'mousemove' event is very costly) so obviously I'm doing something wrong.</p>
  
  <p>My question is:</p>
  
  <ol>
  <li><p>Are there more elegant ways to achieve read-only behaivour from outside <code>MyService</code>.</p></li>
  <li><p>Why is there no such focus on hiding unnecessary logic in Angular2 than in other OOP languages such as Java/C#/PHP.</p></li>
  </ol>