JS Classes and Event Handlers?

JS Classes and Event Handlers?
0.0 0

#1

So im just starting to really use Object Oriented Methods in my code, figured better now than later.

However…im trying to really think of when things should be in my class, vs separate functions vs just on the "document.onready()"

That being said, im doing this for Tic-Tac-Toe (my first implementation was honestly kinda hacky) so I wanted to rewrite it from scratch and make it something “worth showing off”.

So for something like a “Board” class, (in which I return whats in the tile/if the tile is empty/etc…) should I be placing event handlers in this class (Im instantiating the class of course) or is it better to put them within the document.ready scope?

Trying to figure out best practices here, I’d PREFER to put them in the class, but is that even really an option?


#2

Yes, you can make a class called Board :slight_smile:
class Board {
constructor(){
// init here all fields
}
playNextMove(pos, type){
// logic for next turn
}
}

You see the pattern here. Rewrite it so then you can instantiate Board with :
var myBoard = new Board();
myBoard.playNextMove( … );

Hope I gave you some insights :slight_smile: