如何用多个继承属性在Javascript中表示分层产品目录[分类法]

How to represent hierarchical product catalog [taxonomy] in Javascript with multiple inheritted attributes?

本文关键字:分层 表示 产品目录 分类法 Javascript 何用多 继承 属性      更新时间:2023-09-26

我的产品目录是一个经典的树层次结构,具有不同级别的分组。在每个分组级别,产品将共享类型为value的属性。在树的底层,会有继承了上面层所有属性的产品,再加上它们自己的独特属性。

我设想使用两个树视图,一个用于节点,另一个用于属性;将属性类型:值对拖动到produt类别的不同级别。

然后我将使用这个对象树来搜索具有特定属性对的产品。。。并相应地进行显示;因为他们将继承所有属性?

我可以通过实例化每个级别的子类来在Javascript中轻松地表示这一点吗?

类似的模型可以用于层次属性(类型:值)吗?例如制造商->范围->系列


Node = function (txt) { this.level = txt; this.child = []; }
var node = new Array();
node[0] = new Node ("root");
// Add first level
node[1] = new Node ("Paper");
node[0].child.push(node[0]);
// Add second level
node[2] = new Node ("Cut Paper");
node[1].child.push(node[2]);
// Add third level
node[3] = new Node ("A4 Paper");
node[2].child.push(node[3]);
// Add fourth level
node[4] = new Node ("ABC12345");
node[3].child.push(node[4]);
node[3]["Length"]="297mm";
node[3]["Width"]="210mm";
node[4]["Weight"]="80gsm";

可能是这样的?

    function Node ( text ) {
        this.level = text;
        this.child = {};
        this.parent = false;
    }
    Node.prototype = {
        addChild : function  ( text ) {
            this.child[text] = new Node ( text );
            this.child[text].parent = this;
            return this;
        }
    }
    var root = new Node ("root")
            .addChild("Paper"),
        paper = root.child["Paper"]
            .addChild("Cut Paper"),
        cut_paper = paper.child["Cut Paper"]
            .addChild("A4 Paper"),
        a4_paper = cut_paper.child["A4 Paper"]
            .addChild("ABC12345");
        abc12345 = a4_paper.child["ABC12345"];
        a4_paper.Length = "297mm";
        a4_paper.Width = "210mm";
        abc12345.Weight = "80gsm";
        console.log( root, paper, cut_paper, a4_paper, abc12345);

我最近想出了一些创建树结构的想法。可能对您有用,请参阅此jsfiddle