title: Cannot read property 'push' of undefined Typescript / Angular 6
body: <p>I'm Implementing small chat application using socket.io gladly it's working fine. But my concern is when I got a new chat I need to assign it to string array to display in listview.</p>
  
  <p>I have simply define array as <strong>"messages: string[] = [];"</strong> and pushed sample string on page load and it's working fine.But when I got new message from a socket <strong>this.socket.on('newmessage', function (data)</strong> method will fire and can read new message.those all things working correctly.</p>
  
  <p>But When I push new string into my <strong>"messages: string[] = [];"</strong> array. I'm getting 'Cannot read property 'push' of undefined' error.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>import { Component, OnInit} from '@angular/core';
  import * as io from 'socket.io-client';
  
  @Component({
    selector: 'app-chatbox',
    templateUrl: './chatbox.component.html',
    styleUrls: ['./chatbox.component.css'],
  
  })
  
  export class ChatboxComponent implements OnInit {
    socket;
    messages: string[] = [];
    
    constructor() { this.socket = io.connect('http://localhost:8000'); }
  
    ngOnInit() {
      this.initializeChatServer();
    }
  
    initializeChatServer() {
    
      this.messages.push( 'test 55');//This line works
  
      this.socket.on('newmessage', function (data) {
        console.log('message -&gt; ' + data.nick + '&gt;' + data.msg);     
        this.messages.push(data.msg); //Cannot read property 'push' of undefined
      }); 
      
    }
  
  }</code></pre>
  </div>
  </div>
  </p>