Possible Duplicate:
Nested function parameters and 'this' context in Javascript
I'm currently having problems to design my JS classes as desired due to a problem using 'this' in a nested class function call. Don't know how to describe it better so here's a sample what I mean.
test.html
<!DOCTYPE html>
<html class="main" lang="en">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="test.js"></script>
<script type="text/javascript">
function doIt() {
var myTestClass = new TestClass();
}
</script>
</head>
<body>
<button onclick="doIt();">Do it!</button>
</body>
</html>
test.js
function TestClass() {
// this is working
this.alertValue('This works');
// this is not working
setTimeout(function(){this.alertValue('This does not work!')}, 1000);
}
TestClass.prototype.alertValue = function(value) {
alert('Value is: ' + value);
}
Of course this is just a simplified example to demonstrate the problem I mean. So how could I use the 'this' identifier within the function inside the setTimeout call or how would be a better / the correct way to achieve that?
Thanks a lot for your help in advance! Cheers
thisis set (or "reset" if you will) on every function call.