Is there any way to make “private” variables (those defined in the constructor), available to prototype-defined methods?
TestClass = function(){
var priv
Yes, it's possible. PPF design pattern just solves this.
PPF stands for Private Prototype Functions. Basic PPF solves these issues:
For the first, just:
It's that simple. For example:
// Helper class to store private data.
function Data() {};
// Object constructor
function Point(x, y)
{
// container for private vars: all private vars go here
// we want x, y be changeable via methods only
var data = new Data;
data.x = x;
data.y = y;
...
}
// Prototype functions now have access to private instance data
Point.prototype.getX = function(data)
{
return data.x;
}
Point.prototype.getY = function(data)
{
return data.y;
}
...
Read the full story here:
PPF Design Pattern