When looking at how the variable productId
is encapsulated in the code snippet below, can we be certain that calls to ProductId.get()
will always return the previously set value or is there a possibility that the value has been garbage collected and is now undefined?
var ProductId = (function () {
var productId = -1;
return {
get: function () {
return productId;
},
set: function (val) {
productId = parseInt(val, 10);
}
};
})();
For instance, if I were to execute ProductId.set(1234)
followed by ProductId.get()
some time later, could the value have been garbage collected? It appears that there may not be a direct reference to the encapsulated variable productId, potentially leading to it being garbage collected.