If I understand correctly, you want the result of the function call to be a stateful object that you can set and fetch values from.
I'm also assuming you don't want the object which is returned from the function to be accessible anywhere else in the parent object.
In order to do this, you'll need to change the way your object is created.
var Factory = function () {
var _priv = {
x: "something"
};
return {
F: function () {
return _priv;
}
};
};
var myvar = new Factory();
myvar.F().x; // "something"
myvar.F().x = "hello world"; // "hello world"
So, the creation of the initial object is a bit different, but you have the desired results.
If you don't mind that the returned objects properties are accessible elsewhere on the parent object, you could do this:
var myvar = {
_priv: {
x: "something"
},
F: function () {
return this._priv;
}
};
myvar.F().x; // "something"
myvar.F().x = "hello world"; // "hello world"
The downside being that the _priv
property is also accessible on the main object:
myvar._priv.x; // "hello world"
Does this answer your question?