title: Difference between self and this in javascript and when to use either of them
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/962033/what-underlies-this-javascript-idiom-var-self-this">What underlies this JavaScript idiom: var self = this?</a>  </p>
  </blockquote>
  
  
  
  <p>I am confused that when to use self and this in javascript.<br>
  I know that <em>this</em> refers to current context and <em>self</em> refers to current window.<br>
  As I am developing an application in Titanium. I want to know when to use <em>self</em> or <em>this</em>
  OR does is there is any concept of <em>self</em> in titanium development.</p>
  
  <p>here is the sample code that i am doing in my titanium commonJS module</p>
  
  <pre><code>var auth = require('/SDKTest/auth');
  var nodeAPI = require('/SDKTest/nodeAPI');
  function myAPI() {
       this.auth = auth;
       this.nodeAPI = nodeAPI;
       return this;
      }
  module.exports = myAPI;
  </code></pre>
  
  <p>This code works, but can I use <em>self</em> in place of <em>this</em> ? 
  And instead of using <em>this</em> i can create a namespace and do something like this:</p>
  
  <pre><code>  function myAPI() {
       var api = {};
       api.auth = auth;
       api.nodeAPI = nodeAPI;
       return api;
      }
  </code></pre>
  
  <p>both the approaches work but what is the use of using <em>this</em> over here</p>