title: Typescript : Lost scope of 'this'
body: <p>I'm trying to assign a value to myImage. When I look at the js target file myImage doesn't even exist. Obviously this throws an error. How do I preserve the scope of this within typescript classes?</p>
  
  <p>What I'm trying to do here is load an image using the Jimp library. Then have a reference to that loaded image to perform operations on, for example resize is a method of a loaded image inside Jimp, so I'd like to be able to call i.myImage.resize(100,100). </p>
  
  <pre><code>"use strict";
  var Promise = require('bluebird');
  var Jimp = require("jimp/jimp");
  
  
  class Image{
      public myImage:any;
  
      constructor(newImage:string){
  
           Promise.all([new Jimp(newImage)]).then(function(img){
              this.myImage = img;
  
           }).catch(function(e){
               console.log(e);
           })
         }
      }
  
  var i = new Image('./jd.jpg');
  
  console.log(i.myImage)
  </code></pre>
  
  <p>The output: </p>
  
  <pre><code>undefined
  [TypeError: Cannot set property 'myImage' of undefined]
  </code></pre>
  
  <p>With Callbacks : </p>
  
  <pre><code>var Jimp = require("jimp/jimp");
  
  
  class Image {
  
      public myImage: any;
  
      constructor(newImage: string, typeOfImage: string) {
          var self = this;
          new Jimp(newImage, function(e,img) {
             self.myImage = img;
          });
       }
  
  }
  
  var i = new Image('./jd.jpg');
  
  console.log(i.myImage) // outputs undefined
  </code></pre>