When a function needs to use a value that remains unchanged during the call process, it is more convenient to use the function object attributes than to define global variables (which will make the namespace more dispersed. For example, suppose we want to write a function so that it returns a unique integer when called. This function cannot return the same value twice. To do this, it needs to save the returned value, and this information must remain unchanged throughout the function call process. Although we can store this information in a global variable, since this information is used by the function itself, we do not need to use global variables. The best way is to store information in the attributes of the function object. The following is an example. If this function is called, it returns a unique integer:
Uniqueinteger. Counter = 0; function uniqueinteger () {return uniqueinteger. Counter ++ ;}