Updated: This is an update to my previous question that was somewhat off topic as to what this StackExchange is aiming for. But I have a follow up question to the previous inquiry about this subject.
Object Model:
var Soldier;
Soldier = (function() {
"use strict";
function Soldier() {
var privateVar = "privateValue";
this.methodToGetPrivateValue = function() {
return privateVar;
}
}
var sharedPrivateVar = "sharedPrivateValue";
function sharedPrivateMethod() {
// I want to get value `privateVar`
}
Soldier.prototype = {
publicVar: "publicValue",
publicMethod: function() {
return this.publicVar;
},
sharedPrivate: function() {
return sharedPrivateVar;
}
}
return Soldier;
})();
var marine = new Soldier();
So my updated question to make this topic more a proper question is if there is anyway to get a sharedPrivateMethod defined in this way to be able to access the private variable in the above setup?
The reason I am asking is that the sharedPrivateMethod is totaly invisible to the instanced object. While the function defined inside Soldier() is accessible to the instance because of the this.method = function(). I dont know if it has any real use at the moment but would be interesting to see if it was possible somehow.
privatevariables areprivate static.Testfunction will only execute once, so your "private" variables are shared between all instances ofnew Test(). This will not waste memory.