- If we come from a class-based, strongly typed object-oriented language (like Java, C++ or C#) WE assume that we are trying to create a variable or method associated to a "type" but not to an instance.
- For example the sample code is given below:
- If we call that function several time, you'll see the counter is being incremented.
- Maybe the better solution using the global namespace with a global variable.
We will be getting same output except, this time, the incremented value is returned, instead of displayed.
Read AlsoScope variable using AngularJS
We can do it through an IIFE (Immediately Invoked Function Expression):
We can use arguments.callee to store "static" variables (this is useful in anonymous function too):
If we want to declare static variables for creating constants in our application then we found following as most simplistic approach