I know there's no class in JS, but there's something like class, what is really weird for me. I'm quiet new to JS, and I'm trying to build a simple "class". This is how I tried:
Game = new function() {
this.action_finish = 0;
this.thick = function() {
$('#action-finish').text(Game.action_finish--);
setTimeout(this.thick, 1000);
};
};
This is how it worked for me, and I'm wondering, why this.action_finish
(returns NaN) does not work in thick
function instead of Game.action_finish
. Could anyone explain why does it work like this? I've been developing in PHP and C# for years an this approach is hard for me to understand.
Ps.: I don't need more instances of this class, and I'd like to use it like a class that's why I'm using the new keyword.
Edit: This is how I'm using it:
Game.action_finish = 10;
Game.thick();
Edit: Finally I solved my problem. As I tried to define my issue wasnt about the methods of declaring a class, but of reaching a class variable.
Here's the solution in case if anyone has the same problem:
this.thick.bind(this)
A little explanation can be found here: http://javascript.info/tutorial/binding