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);