title: Javascript variable lifecycle
body: <p>I'm learning Javascript and I cannot understand why this code works:</p>
  
  <pre><code>function getObj()
  {
     var objAddress = 
     {
        address: "Client Address",
        getAddress: function() {
           return this.address;
        },
        setAddress: function(newAddress)
        {
           this.address = newAddress;
        }
     };
  
     var objClient =
     {
        name: "Client name",
        getAddress: function()
        {
           return objAddress.getAddress();
        },
        setAddress: function(newAddress) {
           objAddress.setAddress(newAddress);
        }
     };
  
     return objClient;
  }
  
  gObj = getObj();
  console.log(gObj.getAddress()); // Will print "Client Address"
  gObj.setAddress("xpto");
  console.log(gObj.getAddress()); // Will print "xpto"
  </code></pre>
  
  <p>I thought it would not work since <code>getAddress()</code> calls another method of an object that should not exist after leaving the function. But, as this is working, I presume that the object <code>objAddress</code> still exists even after quiting the <code>getObj</code> function.</p>
  
  <p>Outside the function, how can the <code>gObj.getAddress()</code> work? </p>