I came across this code when I am working on a project. I am not able to understand why 'this' is used in the createNewGame method? Btw I am using socket.io and express js for my project.
var io
var gameSocket
var gamesInSession = []
const initializeGame = (sio, socket) => {
// initialize global variables.
io = sio
gameSocket = socket
gamesInSession.push(gameSocket)
// User creates new game room after clicking 'submit' on the frontend
gameSocket.on("createNewGame", createNewGame);
...
}
function createNewGame(gameId) {
// Return the Room ID (gameId) and the socket ID (mySocketId) to the browser client
this.emit('createNewGame', {gameId: gameId, mySocketId: this.id}); //What does this refer too here?
this.join(gameId)
}