0

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)
}
Sachin
  • 446
  • 4
  • 9
  • 1
    you can "console.log(this)", to see what it is – Loïc May 12 '21 at 13:06
  • `mySocketId: this.id` — the name of the property the value is being copied too might be a clue – Quentin May 12 '21 at 13:07
  • I log and it is giving me something like this: Socket { _events: [Object: null prototype] { createNewGame: [Function: createNewGame], }, – Sachin May 12 '21 at 13:35

0 Answers0