Question: I have a method that uses the [`setTimeout`](https://developer.mozilla.org/en/window.setTimeout) function and makes a call to another method. On initial load method 2 works fine. However, after the timeout, I get an error that says `method2` is undefined. What am I doing wrong here?

ex:

```
test.prototype.method = function()
{
    //method2 returns image based on the id passed
    this.method2('useSomeElement').src = "http://www.some.url";
    timeDelay = window.setTimeout(this.method, 5000);
};

test.prototype.method2 = function(name) {
    for (var i = 0; i < document.images.length; i++) {
        if (document.images[i].id.indexOf(name) > 1) {
            return document.images[i];
        }
    }
};

```

Answer: 
in es6 you can do it like this 

```
window.setTimeout(() => {
    this.foo();
}, 1000);   

```
> 
> I get an error that says method2 is undefined
> 
> 
> 

Yes, when you slice off `this.method` from its owner and pass the function alone to `setTimeout`, you lose the association that sets `this`, so `this` in `method()` is equal to the global object `window`.

See [this answer](https://stackoverflow.com/questions/585840/implement-map-in-javascript-that-supports-object-methods-as-mapped-functions/585918#585918) for an explanation of the surprising way `this` actually works in JavaScript.