title: Javascript/Typescript Closure Problem - Returning an object's function as a variable
body: <p>I am still very confused about JavaScript/TypeScript closure after reading through a number of examples on the web.</p>
  <p>Here is the code that I am having trouble with:</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>let obj = {
    message: '222',  
    printMessage: function() {
      return this.message
    },
  }
  
  console.log(obj.printMessage()); // 222
  let func = obj.printMessage;
  console.log(func());             // undefined</code></pre>
  </div>
  </div>
  </p>
  <p>When I run this code, I am getting error <code>Cannot read property 'message' of undefined</code>.</p>
  <p>I know I can run <code>obj.printMessage()</code> directly, but that's not what I want to do. I want to be able to return the <code>printMessage</code> function as a variable and eventually get called by another function.</p>
  <p>I feel that the problem is related to closure, I just don't know how to approach it.</p>
  <p>Many thanks for your input.</p>
  <p>===========================================</p>
  <p>Update:</p>
  <p>I found the solution to my problem after tinkering with the code for a bit longer.</p>
  <p>Here is my solution.</p>
  <pre><code>let obj = {
    message: '222',  
    printMessage: function() {
      return this.message
    },
    getPrintMessage: function () {
        return () =&gt; this.printMessage()
    }
  }
  
  console.log(obj.printMessage()); // 222
  let func = obj.getPrintMessage();
  console.log(func());
  </code></pre>
  <p>I created another function to return an anonymous function that calls <code>this.printMessage()</code>. This way I can access the function from outside of the object.</p>
  <p>Thanks for everyone's input.</p>