lai0n lai0n - 21 days ago 7
Javascript Question

this.addClient is not a function

this.server = net.createServer(this.onAccept);
this.server.listen(this.port);
}

Server.prototype.onAccept = function () {
var client = new Client();

this.addClient(client);
};

Server.prototype.addClient = function (client) {
this.clients.push(client);
};


I'm getting this error on node.js: this.addClient is not a function on line 27

i don't know why this is occurring.

Answer

Most likely your this pointer isn't what you expect, you can bind it explicitly

this.server = net.createServer(this.onAccept.bind(this);
  this.server.listen(this.port);
}
Comments