Answer the question
In order to leave comments, you need to log in
Passing an object by reference, working with memory?
I read https://learn.javascript.ru/object-reference, but I still have questions, I hope you can help me figure it out.
On the server node with a socket. Let's say we have:
var globalVariable = {
user111: {
id: 111,
name: 'Vasya'
},
user222: {
id: 222,
name: 'Petya'
}
};
//слушаем ивент на ренейм юзера
.on('rename', obj => {
//где obj = { id: [user_id], newName: [string] }
//изменяем глобальный объект, ссылкой на вновь прибывший ?
globalVariable['user'+obj.id].name = obj.newName;
});
//у юзера есть еще свойство customObj, который является объектом
.on('someEvent', obj => {
//где obj = { propone: 'someValue', proptwo: 'someValue' }
//предположим что каким-то магическим образом знаем ид юзера
globalVariable.user111.customObj = obj;
});
Answer the question
In order to leave comments, you need to log in
Objects are assigned by reference, simple types are assigned by value.
If obj.newName is a string, then globalVariable['user'+obj.id].name will be set to a string
Second case, obj.newName is an object, in which case globalVariable['user'+obj.id].name will be set to a reference to the newName object itself, the fact that this object can be simultaneously a property of obj does not prevent the obj garbage collector from removing it.
In this particular case, assigning obj = null would just be an extra useless operation.
how to correctly assign objects to a global object so that there are no references to it? Example:
This is a very general question and there is no short answer.
after all, it will gobble up all the memory, astoo, too childish statement, in relation specifically to your piece of code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question