Last active
August 29, 2015 14:27
-
-
Save kulakowka/6fdb1f84a3130dd157f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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