// 1. "socket" all lowercaseconstsocket=io("ws://localhost:3000");// 2. "socket" all lowercasesocket.on("connect",()=>{// 3. "socket" all lowercaseconsole.log(socket.id);});
Basically the listener is a function expression (arrow functions are always function expressions) - and a function has access to all the variables in the scope that created it (in this case socket).
constsocket=io("ws://localhost:3000");socket.on("connect",connectListener);// function declaration...functionconnectListener(){// ...also has access to the enclosing scopeconsole.log(socket.id);}
Finally graduated University and ready to start learning on the job. Recently started as an graduate and outside of work hours I'm still trying to fit in time for my own projects and collabs.
socket.on(eventName, listener)
Socket#id
How does that work?
Watch for the casing on the variable names:
Basically the
listener
is a function expression (arrow functions are always function expressions) - and a function has access to all the variables in the scope that created it (in this casesocket
).MDN: Closures
A function declaration
would work as well.
Thank you. I think it was just my phone adding the caps but I'll double check my code. Thank you