I\'ve create a prototype based class Person
that opens a WebSocket connection and defines callback functions as prototype methods.
Because inside the ca
self = this
Your creating a global variable, that's why your code is broken.
Also trying to reference self
inside the prototype does not work, use this
function Person(name){
this.name = name
}
Person.prototype = {
openConnection : function(host, port){
this.pointCount = 0
this.ws = new WebSocket("ws://" + host + ":" + port)
this.ws.onopen = this.onOpenConnection.bind(this)
},
constructor: Person,
onOpenConnection : function() {
console.log(this) // prints the person
this.ws.send(this.name) // works only if one person exists
}
}