title: javascript passing this inside an object method
body: <p>this code works, but my question is I dont understand the purpose of <code>var that = this</code>. Why do I need to reference it like that to pass it to setInterval. I read about 'this' in <a href="http://www.sitepoint.com/what-is-this-in-javascript/" rel="nofollow">http://www.sitepoint.com/what-is-this-in-javascript/</a>, but it doesn't really answer my question</p>
  
  <p>my JavaScript code</p>
  
  <pre><code>function spinClass(imageSource, height, width, forward, el){
  
  this.src = imageSource;
  this.spinFoward = forward;
  this.element = document.getElementById(el);
  this.height = height;
  this.width = width;
  this.d = 0;
  
  
  var img = document.createElement("img");
  img.setAttribute('src', this.src);
  img.setAttribute('height', this.height);
  img.setAttribute('width', this.width);
  
  this.element.appendChild(img);
  
  this.letSpin = function letSpin(){
      //alert(this.d);
      var that = this;
      img.style.transform = "rotate(" + this.d + "deg)";
      img.style.WebkitTransform= "rotate(" + this.d + "deg)";
      img.style.MozTransform= "rotate(" + this.d + "deg)";
      img.style.msTransform= "rotate(" + this.d + "deg)";
      img.style.OTransform= "rotate(" + this.d + "deg)";
  
      //alert(this.spinFoward);
      if (this.spinFoward == true){
          this.d++;
      }else{
          this.d--;
      }
  
      setInterval(function(){that.letSpin();}, 20);
  };
  </code></pre>
  
  <p>}</p>