Reputation: 2709
Besides readability, is there any reason to avoid or not program static variables using this style?
function not_global()
{
var print;
(function() {
var static = value;
print = function(value) {
static = value;
}
})();
}
Upvotes: 1
Views: 182
Reputation: 208705
I would avoid that style. It isn't very explicit so it becomes difficult without comments to tell that print
being exposed outside of the IIFE is intentional. Instead, I would suggest returning print
from the function and assigning it there:
var print = (function()
{
var text_log=document.getElementById('text_log_text');
return function(string)
{
text_log.innerHTML+='<br />'+string;
};
})();
Note that polluting the global namespace is discouraged, so really the best answer here is to just not have print
exposed outside. This answer assumes you have already thought through that, and either your IIFE is actually nested in another function (so it isn't polluting the global namespace), or you really do want print
to be global.
Upvotes: 1