Not sure I completely understand answers to similar questions that I found here, so trying to be absolutely sure:
I would like to have a local variable in a function, initialized only once (similar to static variables in strongly-typed languages such as C, C++, etc).
Of course, I could declare it globally, but it seems better practice to have it within the scope of that function, since it is not used anywhere else.
Now, here is what I do:
function func(data) {
func.PARAMS = [
{"name": "from", "size": 160, "indexed": true},
{"name": "input", "size": 256, "indexed": false},
{"name": "output", "size": 256, "indexed": false},
];
...
}
And my question is, will func.PARAMS
indeed be initialized only once, or will it be initialized every time the function is called?
According to some of the answers that I found (this one for example), I need to precede the initialization with something like:
if (typeof func.PARAMS == 'undefined')
This "supplemental" would be irrelevant in strongly-typed languages of course, so I just want to be sure that it is absolutely necessary in order to ensure "static behavior" (i.e., one-time initialization).
typeof func.PARAMS == 'undefined'
, according tofunc
,PARAMS
would be set every timefunct
is called. – Snowden