Consider the following code in java
class x {
private boolean a;
public void DoSomethingUsingA() {
....... // "a" referenced here
}
}
x X = new x();
x Y = new x();
Each of X and Y have DoSomethingUsingA(), but the function is only created once in memory.
Now if you try this in javascript
function x() {
var a;
this.DoSomethingUsingA= function() {
........ // "a" referenced here
}
}
var X = new x();
var Y = new y();
DoSomethingUsingA() here is defined twice in memory for each object X & Y.
Another attempt would be
var x = (function() {
var a;
function constructor() {
........
}
constructor.prototype.DoSomethingUsingA = function() {
....... // "a" referenced here
}
return constructor;
}())
var X = new x();
var Y = new y();
now DoSomethingUsingA() is only defined once in memory, but the private variables are static across all derived objects
Question: How can I have the functions for all derived objects be defined only once in memory, while having access to non static private variables for each object.