オブジェクトリテラルをと呼ばれるフレームワークメソッドに渡しますsupportP()
。このオブジェクトリテラルには_p
、そのメンバーがプライベートであることを示すという特別なプロパティがあります。オブジェクトリテラル内のwithから、を介してアクセスできますthis._p
。ただし、オブジェクトリテラルを「外部」スコープに渡す場合、コピーしません_p
。現在は省略により非公開になっています。パブリックメンバーメソッドから_pにアクセスするために、を使用して元のオブジェクトにバインドし、をbind()
介して_pにアクセスできるようにしthis
ます。
これは機能しますか?他に考慮すべきことはありますか?テストする前にフィードバックが必要でした。
以下は関連するスニペットです。
/*$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);