Skip to content

Instantly share code, notes, and snippets.

@kulakowka
Last active August 29, 2015 14:27
Show Gist options
  • Save kulakowka/6fdb1f84a3130dd157f0 to your computer and use it in GitHub Desktop.
Save kulakowka/6fdb1f84a3130dd157f0 to your computer and use it in GitHub Desktop.
function ObjFactory(initialPrivateVariableForAll) {
var privateVariableForAll = initialPrivateVariableForAll;
return function ObjConstructor(initialPrivateVariableForInstance) {
var privateVariableForInstance = initialPrivateVariableForInstance;
return {
getInitialPrivateVariableForAll: function() {
return initialPrivateVariableForAll;
},
getInitialPrivateVariableForInstance: function() {
return initialPrivateVariableForInstance;
},
getPrivateVariableForAll: function() {
return privateVariableForAll;
},
getPrivateVariableForInstance: function() {
return privateVariableForInstance;
},
setPrivateVariableForInstance: function(value) {
privateVariableForInstance = value;
},
setPrivateVariableForAll: function(value) {
privateVariableForAll = value;
},
};
};
}
var Obj = new ObjFactory('initialPrivateVariableForAll');
var o = new Obj('initialPrivateVariableForInstance1');
var b = new Obj('initialPrivateVariableForInstance2');
console.log(o.getPrivateVariableForAll());
console.log(o.getPrivateVariableForInstance());
console.log(b.getPrivateVariableForAll());
console.log(b.getPrivateVariableForInstance());
console.log('++++++++++++++++++++++++++++++++++++++++++++++++++++++++');
b.setPrivateVariableForInstance('changedPrivateVariableForInstance2');
b.setPrivateVariableForAll('changedPrivateVariableForAll');
console.log(o.getPrivateVariableForAll());
console.log(o.getPrivateVariableForInstance());
console.log(b.getPrivateVariableForAll());
console.log(b.getPrivateVariableForInstance());
console.log('++++++++++++++++++++++initial++++++++++++++++++++++++++++++++++');
console.log(o.getInitialPrivateVariableForAll());
console.log(o.getInitialPrivateVariableForInstance());
console.log(b.getInitialPrivateVariableForAll());
console.log(b.getInitialPrivateVariableForInstance());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment