Say I have a javascript function/class called Foo and it has a property called bar. I want the value of bar to be supplied when the class is instantiated, e.g:
var myFoo = new Foo(5);
would set myFoo.bar to 5.
If I make bar a public variable, then this works, e.g:
function Foo(bar)
{
this.bar = bar;
}
But if I want to make it private, e.g:
function Foo(bar)
{
var bar;
}
Then how would I set the value of the private variable bar such that its available to all internal functions of foo?
You have to put all functions that need to access the private variable inside the constructor: