I pass an object literal into a framework method called supportP(). This object literal has a special property called _p which denotes that the members of it are private. From with in the object literal it can be accessed via this._p. However when I pass the object literal into the “outer” scope I do not copy _p. It has now been made private by omission. In order to access _p from public member methods I bind them to the original object using bind() so they still have access to _p via this.
Will this work? Are there other things to consider? Wanted some feedback before I tested it out.
Below are relevant snippets.
/*$A.supportP
**
**
**
*/
$A.supportP = function (o, not_singleton) {
var oo
key;
SupportList[o.Name] = {};
if (not_singleton) {
// ignore this section
} else { // *look here - isFunc returns true if a function
for (key in o) {
if ((key !== '_p') && (isFunc(o[key])) {
oo[key] = o[key].bind(o);
} else if (key !== '_p') {
oo[key] = o[key];
} else {
// private (_p) - anything to do here?
}
}
return oo;
}
};
/*$A.test
**
**
**
*/
var singleton_object = $A.supportP({
_p: 'I am private',
Name: 'test',
publik_func: function () {
// this will refer to this object so that it can access _p
// this._p is accessible here due to binding
}
}, false);
Yes, you will be able to access the “private” property via
this._p.You are cloning the object. Yet, the method on it has no access to it – it is bound to the “old” object whose properties will not reflect the changes on the copy. I am not sure whether this is by design or by accident.
For strict privateness, you will need to use closures with local variables. Properties can never be made private.
Another solution, using two distinct objects (one hidden) which are passed into a framework method: