I pass the object literal to a structure method called supportP() . This object literal has the special property _p , which means that its members are private. From with in an object literal can be obtained through this._p . However, when I pass the object literal to the "external" volume, I do not copy _p . Now it was closed by inaction. To access _p from public member methods, I bind them to the original object using bind() so that they still have access to _p through this .
Will this work? Are there any other questions? Some feedback is required before I checked it.
Below are the 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);
user1637281
source share