CopyAndPaste

random programming notes

Javascript Function Properties

Sometimes you need variable in a function to persist across invocations. You can always define global variable to do that. However, that clusters up the namespace.

A better way to do that is to define a function property accessible only to that particular function. Example:

1
2
3
4
5
6
someFunction.counter = 0;

function someFunction() {
  return someFunction.counter;
  // the counter is like a static function variable in C
}