0

簡単に言えば

オブジェクトで構成されたツリー構造があります。

そのツリーを構築し、各オブジェクトに親への参照を追加することは可能ですか?

参照がオブジェクトで機能することは知っていますが、その場合に機能するかどうかはわかりませんか?

こんな感じで書けるようになりたいです

currentLevel = this.getParent();

別の例は

this.getChildList().addChild({name: test,parent: this}) 

コピーを作成せずに、最初のツリーから複数のツリーを作成します。

2番目の質問

参照は配列でどのように機能しますか? それらはオブジェクトと見なされますか、それともコンテンツに依存しますか?

3番目の質問

文字列の JSON シリアライゼーションを介してブラウザのキャッシュにツリーを保存すると、参照が破棄されますか?

4

3 に答える 3

2

オーケー、そこにはおそらくフレームワークがありますが、JSON シリアライゼーションとその逆をサポートする簡単なものを書きました (独自のメソッドを介して)。ニールの答えから基本的なインスピレーションを得ました。例

var a = new MyTreeNode('a'), // make some nodes
    b = new MyTreeNode('b'),
    c = new MyTreeNode('c');

a.addChild(b).addChild(c); // a parent of b parent of c

c.getParent() === b; // true

var str = a.toJSON(); // "{"nodeName":"a","childNodes":[{"nodeName":"b","childNodes":[{"nodeName":"c","childNodes":[]}]}]}"

MyTreeNode.parseJSON(str); // MyTreeNode (same structure as before)

完全なコード

/* MyTreeNode(String nodeName)
Instance Properties
 - nodeName,   String
 - childNodes, Array of MyTreeNodes
 - parentNode, MyTreeNode
Instance Methods
 - addChild(MyTreeNode node),    child MyTreeNode
 - removeChild(MyTreeNode node), child MyTreeNode
 - getParent,                    parent MyTreeNode
 - getChildList,                 Array of MyTreeNodes
 - serialise,                    JSON-safe Object
 - toJSON,                       String
Constructor Methods
 - deserialise(Object serialised), MyTreeNode
 - parseJSON(String JSONString),   MyTreeNode
*/
var MyTreeNode = (function () {
    function MyTreeNode(nodeName) {
        nodeName && (this.nodeName = nodeName);
        this.childNodes = [];
    }

    MyTreeNode.prototype.parentNode = null;
    MyTreeNode.prototype.childNodes = [];
    MyTreeNode.prototype.nodeName = '';

    // getters
    MyTreeNode.prototype.getChildList = function () {
        return this.childNodes = [];
    };
    MyTreeNode.prototype.getParent = function () {
        return this.parentNode;
    };

    // add/remove
    MyTreeNode.prototype.removeChild = function (node) {
        var i = this.childNodes.indexOf(node);
        if (node.parentNode !== this || i == -1)
            throw new ReferenceError('node is not a child of this');
        this.childNodes.splice(i, 1);
        node.parentNode = null;
        return node;
    };
    MyTreeNode.prototype.addChild = function (node) {
        if (node.parentNode) node.parentNode.removeChild(node);
        node.parentNode = this;
        this.childNodes.push(node);
        return node;
    };

    // JSON
    MyTreeNode.prototype.serialise = function () {
        var o = {
            nodeName: this.nodeName,
            childNodes: []
        }, i;
        for (i = 0; i < this.childNodes.length; ++i) {
            o.childNodes.push(this.childNodes[i].serialise());
        }
        return o;
    };
    MyTreeNode.prototype.toJSON = function () {
        return JSON.stringify(this.serialise());
    };
    MyTreeNode.deserialise = function (o) {
        var p = new MyTreeNode(o.nodeName), i;
        for (i = 0; i < o.childNodes.length; ++i) {
            p.addChild(MyTreeNode.deserialise(o.childNodes[i]));
        }
        return p;
    };
    MyTreeNode.parseJSON = function (str) {
        var o = JSON.parse(str);
        return MyTreeNode.deserialise(o);
    };
    return MyTreeNode;
}());
于 2013-06-14T15:39:40.140 に答える
1

オブジェクトをトラバースして、すべてのサブオブジェクトに親プロパティを追加できます。

function addParents(obj) {
    var name;
    for (name in obj) {
        if (typeof obj[name] === "object") {
            addParents(obj[name]);
            obj[name].parent = obj;
        }
    }
}

var obj = {
    g: {
        k: [
            {
                r : 1
            },
            {
                r : 1
            }
        ],
        j: {
            h: 1
        }
    }
};

addParents(obj);

console.log(obj.g.parent === obj); //true
console.log(obj.g.k.parent === obj.g); //true
console.log(obj.g.k[1].parent === obj.g.k); //true
console.log(obj.g.j.parent === obj.g); //true

後でオブジェクトを追加したい場合は、次のようなものを使用できます。

function addChild(obj, child, name){
    obj[name] = child;
    child.parent = obj;
}

addChild(obj.g, {t:1}, "xy");

console.log(obj.g.xy.parent === obj.g); //true

フィドル

于 2013-06-14T15:23:08.397 に答える