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
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…