i saw that people declare functions in two ways:
1.
application.onConnect = function(clientObj, uid,gameName) {
clientObj.functionname= function() { ... }
}
2.
Client.prototype.functionName = function() { ... }
what’s the difference ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The previous answer is incomplete.
The 1st example adds methods ONLY TO THAT SPECIFIC INSTANCE of Client class.
The 2nd example adds methods to all the Class prototype.
This distinction is very important for some uses cases.
If for example the client that connects to a room has administrative privileges created by your logic, in onConnect’s client instance you will add methods only to admin’s user client otherwise in each admin method you will need to check if the client is admin or not (which would be ugly)
Sample:
If you do not put the disonnectUser inside the if you can imagine a malicious person could forge a client that would allow him to disconnect any of your users.
I hope this helps.