Answer the question
In order to leave comments, you need to log in
How to achieve object deletion in JS?
Started learning JS, decided to start with a simple chat. Actually there was a problem.
So, I have a ChatServer object, which implements a socket.io object, and implements work with connecting and disconnecting clients.
"use strict";
var SocketServer = require('socket.io')
, Client = require('./Client.js');
class ChatServer {
constructor(app, http) {
var io = SocketServer(http)
, clients = {};
io.on('connection', function(socket) {
console.log('Client ' + socket.id + ' connected');
clients[socket.id] = new Client(app, socket);
socket.on('disconnect', function() {
console.log('Client ' + socket.id + ' disconnected');
clients[socket.id] = undefined;
delete clients[socket.id];
});
});
}
}
module.exports = ChatServer;
"use strict";
class Client {
constructor(app, socket) {
this.app = app;
this.socket = socket;
this.app.on('chat.sendMessage', this.sendMessage.bind(this));
}
sendMessage(message) {
console.log('Client ' + this.socket.id + ' on chat.sendMessage');
this.socket.emit('chat.sendMessage', message);
}
}
module.exports = Client;
clients[socket.id] = undefined;
delete clients[socket.id];
Answer the question
In order to leave comments, you need to log in
clients[socket.id] = undefined;
delete clients[socket.id];
Least,
clients[socket.id] = undefined;
delete clients[socket.id];
delete clients[socket.id];
Is it possible to delete an object in Javascript by removing all references to it?
This kind of works in ARC only.
If in js the garbage collector is responsible for objects, then most likely he will delete them himself.
And the fact that the object continues to catch messages, then apparently it is necessary to prohibit it from doing it in another way, except for deletion
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question