Одной из альтернатив для использования частных методов с доступом к this
является использование методов call
или apply
.
function Restaurant()
{
this.mongoose = 'beans';
this.freedom = {bear:'love',a:'12'};
var myPrivateVar;
var private_stuff = function() // Only visible inside Restaurant()
{
myPrivateVar = "I can set this here!";
this.mongoose = 12;
}
this.use_restroom = function() // use_restroom is visible to all
{
private_stuff();
}
this.buy_food = function() // buy_food is visible to all
{
private_stuff();
}
private_stuff.call(this);
}
var bobbys = new Restaurant();
Конечно, вы бы переместили use_restroom и buy_food в прототипи private_stuff вне конструктора, если вы планируете иметь несколько экземпляров этого объекта.