I had a question regarding the following code. I was creating a nested object and I wanted to create it in one short, concise line. I got the idea from this answer here. I'm not planning on using this code for production either. I am being warned that temp
'might' be a leak. This is simply an example I was able to generate. I do understand that newSeasons
and temp
are being assigned to the same object in memory, that is why all this is possible.
Is this in fact creating a memory leak?
The line in question is:
var newSeasons = temp = {}; temp[yr] = data;
You can test the code here.
function parseData (yr, stat, data) {
// The way I've been taught
var oldSeasons = {};
oldSeasons[yr] = data;
console.log('The way Ive been taught\n');
console.log(oldSeasons);
console.log('\n****************************\n');
// Experimental way
var newSeasons = temp = {}; temp[yr] = data;
console.log('Experimental way');
console.log(newSeasons);
}
var data = {
Pos: '1B',
Age: '33',
G: '116',
stat:'batting',
yr: '2005',
H:'89',
R: '42',
RBI: '48'
};
parseData(data.yr,data.stat,data);