ProtoViewLogic: getRelatedViewLogics()
対象の ProtoViewLogic
と親子関係を結んでいる(relateViewLogics() 参照)ProtoViewLogic
を取得します。
構文
ProtoViewLogic.getRelatedViewLogics() => object
返値: object
対象の ProtoViewLogic
の直接の子である ProtoViewLogic
をプロパティに持つオブジェクトです。
各プロパティ名は対象の ProtoViewLogic
が子の ProtoViewLogic
またはその配列 ProtoViewLogic[]
を参照するプロパティと同名です。
例えば protoviewlogic.foo
および protoviewlogic.bar
にそれぞれ子の ProtoViewLogic
と ProtoViewLogic[]
が設定されている場合、protoviewlogic.getRelatedViewLogics()
の返値は { foo: ProtoViewLogic, bar: ProtoViewLogic[] }
となります。
note
返値のオブジェクトは Object
を継承していません(null-prototype object です)。
例
const { ProtoViewLogic } = await Alier.import("/alier_sys/ProtoViewLogic.js");
console.group("Example of ProtoViewLogic.getRelatedViewLogics()");
const vl = new ProtoViewLogic;
const foo = new ProtoViewLogic;
const bar = [
new ProtoViewLogic,
new ProtoViewLogic,
];
const baz = new ProtoViewLogic;
const qux = new ProtoViewLogic;
console.groupCollapsed("step-1");
// (1) 何も関連付けられていない状態で getRelatedViewLogics() を呼ぶ。
// これは空のオブジェクトを返す。
console.log(vl.getRelatedViewLogics());
// ==> {}
console.groupEnd();
console.groupCollapsed("step-2");
// foo と bar を vl の子にする
vl.relateViewLogics({ foo, bar });
// (2) ProtoViewLogic が関連付けられた状態で getRelatedViewLogics() を呼ぶ。
// これは foo, bar を含むオブジェクトを返す。
console.log(vl.getRelatedViewLogics());
// ==> { foo: ProtoViewLogic, bar: ProtoViewLogic[] }
console.groupEnd();
// baz を foo の子にする
foo.relateViewLogics({ baz });
// qux を bar[1] の子にする
bar[1].relateViewLogics({ qux });
// 応用例: すべての子孫を取得する
const $self = Symbol("self");
const descendantsOf = (vl) => {
const o = vl.getRelatedViewLogics();
o[$self] = vl;
for (const k in o) {
const v = o[k];
if (v instanceof ProtoViewLogic) {
o[k] = descendantsOf(v);
} else {
o[k] = v.map(descendantsOf);
}
}
for (const k in o) {
// o が文字列のキーを持っているなら以下が実行される
return o;
}
// $self しかプロパティを持たないなら縮約する
return vl;
};
console.group("step-3");
// (3) vl に連なる子孫をすべて列挙する
// これは foo, bar, baz, qux を含む。
console.log(descendantsOf(vl));
// ==> {
// foo : {
// baz : ProtoViewLogic,
// Symbol(self): ProtoViewLogic
// },
// bar : [
// ProtoViewLogic,
// {
// qux : ProtoViewLogic,
// Symbol(self): ProtoViewLogic
// }
// ],
// Symbol(self): ProtoViewLogic
// }
console.groupEnd();
console.groupEnd();
解説
対象の ProtoViewLogic
から、直接の子である ProtoViewLogic
またはその配列を参照するプロパティを抽出し、null-prototype オブジェクトとして返します。
この関数は例えば、for-in
ループや Object.entries()
などを利用してそれぞれの子の ProtoViewLogic
に対する処理を行うために利用できます。