I notice that when I run the code in Code Sample 1, that after the first iteration the value of the optional argument ‘data’ is not the set default of {cl={}, ch={}}. Instead it appears to be the value of ‘data’ from the previous iteration, with its sub table(s) retaining their contents. Why does this occur and is it intended?
For comparison Code Sample 2 is the modification I had to make to make it perform as I was expecting it too.
Code Sample 1:
`function loadTable(callback, data={cl={}, ch={}}){
server.log("data = "+http.jsonencode(data));
data.cl.n <- 127;
data.ch.g <- 303;
imp.wakeup( 1, function(){
callback(data);
});
}
function runTest(){
loadTable(function(cols){
runTest();
});
}
runTest();`
Code Output 1:
2017-01-17 19:04:49 UTC+13 [Agent] data = { “cl”: { }, “ch”: { } }
2017-01-17 19:04:50 UTC+13 [Agent] data = { “cl”: { “n”: 127 }, “ch”: { “g”: 303 } }
2017-01-17 19:04:51 UTC+13 [Agent] data = { “cl”: { “n”: 127 }, “ch”: { “g”: 303 } }
2017-01-17 19:04:52 UTC+13 [Agent] data = { “cl”: { “n”: 127 }, “ch”: { “g”: 303 }
…
Code Sample 2:
`function loadTable(callback, data=null){
if(data==null)
data = {cl={}, ch={}};
server.log("data = "+http.jsonencode(data));
data.cl.n <- 127;
data.ch.g <- 303;
imp.wakeup( 1, function(){
callback(data);
});
}
function runTest(){
loadTable(function(cols){
runTest();
});
}
runTest();`
Code Output 2:
2017-01-17 19:01:52 UTC+13 [Agent] data = { “cl”: { }, “ch”: { } }
2017-01-17 19:01:53 UTC+13 [Agent] data = { “cl”: { }, “ch”: { } }
2017-01-17 19:01:54 UTC+13 [Agent] data = { “cl”: { }, “ch”: { } }
2017-01-17 19:01:55 UTC+13 [Agent] data = { “cl”: { }, “ch”: { } }
…