Question: this code works, but my question is I dont understand the purpose of `var that = this`. Why do I need to reference it like that to pass it to setInterval. I read about 'this' in <http://www.sitepoint.com/what-is-this-in-javascript/>, but it doesn't really answer my question

my JavaScript 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);
};

```

}

Answer: 
The value of the [`this` keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) is tied to the `function` it's used within and to how that `function` was called.

That includes both `letSpin()` and the short, anonymous `function` being passed to `setTimeout()`. And, the anonymous `function` won't automatically *inherit* or share the `this` value from `letSpin()` just by its placement.

So, you have to either *capture* the value in a variable with another name.

```
var that = this;

```

Or, [bind the `function`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) so it will use a particular value when it's called.

```
setTimeout(function(){
    this.letSpin();
}.bind(this), 20);

```

And, with `bind`, you can also pass the method without the anonymous `function`.

```
setTimeout(this.letSpin.bind(this), 20);

```
Instantiate object with this function:

```js
function newClass(klass) {
    var obj = new klass;

    $.map(obj, function(value, key) {
        if (typeof  value == "function") {
            obj[key] = value.bind(obj);
        }
    });

    return obj;
}

```

This will do automatic binding of all function, so you will get object in habitual OOP style,
when methods inside objects has context of its object.

So you instantiate you objects not through the:

```
var obj = new spinClass();

```

But:

```
var obj = newClass(spinClass);

```