ALL


  JavaScript: Private variables

The first thing you often hear when talking about JavaScript is often “JavaScript doesn’t have visibility modifiers, hence it doesn’t support private variables”. This is only valid for 50%, because JavaScript indeed doesn’t have visibility modifiers (public, private, friend) but it has closures and function scopes. With these tools we can make variables private. Let’s take the following function:var names = ["Kenneth", "John", "Marc", "Robert"];var lookup = function (index) { return names[index];}alert(lookup(0));As you can see the variable “names&r...

4,746 3       JAVASCRIPT PRIVATE VARIABLE CLOSURE