How do I fix createBar()
so that a private property bar
is created at the same level as the property foo
?
var x = (function() {
var foo = "a";
function createBar() {
this.bar = "b";
}
return {
getFoo: function() {
return foo;
},
getBar: function() {
return bar;
}
}; // end: returned object
}());
This is how the module should work:
x.getFoo(); // returns a
x.getBar(); // returns b
window.bar; // is undefined (but in my example, it is b)
Update:
This is what I am doing now:
var x = (function() {
var data = {}; //private object holding state variables
data.foo = "a"; // one state variable
function createBar() {
data.bar = "b"; // another dynamically created state variable
}
return {
getFoo: function() {
return foo;
},
getBar: function() {
return bar;
}
}; // end: returned object
}());
but somehow I don't like it. What would you do, to share state informations between private functions?