覆盖对象属性值(简单链接列表示例)

时间:2013-01-28 13:07:34

标签: javascript json data-structures

我在JavaScript中做一个简单的链接列表(我是新手),我有以下代码,

var List = function () {
  this.LinkedList = {
    "Head": {}
  };
};
List.prototype = {
  insert: function (element) {
    var Node = this.head();
    while (Node.hasOwnProperty("Node")) {
      Node = this.next(Node);
    }
    Node["Node"] = {
      "element": element
    };
  },
  remove: function (element) {
    var Node = this.head();
    while (Node.element != element) {
      Node = this.next(Node);
    }
    delete Node.element;
    Node = Node.Node; //overwriting Node with Node.Node
  },
  next: function (Node) {
    return Node.Node;
  },
  head: function () {
    return this.LinkedList.Head;
  },
  getList: function () {
    return this.LinkedList;
  }
};

当我进行插入时,它很好,

var myList = new List();
myList.insert(1);
myList.insert(5);
myList.insert(6);
myList.insert(2);

这给了我一个List of,

    {
    "Head": {
        "Node": {
            "element": 1,
            "Node": {
                "element": 5,
                "Node": {
                    "element": 6,
                    "Node": {
                        "element": 2
                    }
                }
            }
        }
    }
}

现在当我删除时,它没有给出正确的列表:

myList.remove(5);

{
    "Head": {
        "Node": {
            "element": 1,
            "Node": {
                "Node": {
                    "element": 6,
                    "Node": {
                        "element": 2
                    }
                }
            }
        }
    }
}

我想要的是这样的:

{
    "Head": {
        "Node": {
            "element": 1,
            "Node": {
                "element": 6,
                "Node": {
                    "element": 2
                }
            }
        }
    }
}

关于如何解决这个问题的任何想法?提前谢谢。

1 个答案:

答案 0 :(得分:1)

这是因为Node = Node.Node没有将下一个节点指定为当前节点。您只是将Node.Node分配给变量Node。有了它,你不会覆盖。从某种意义上说,你只能获得“阅读权限”。

要解决此问题并获得传递引用的好处,修改变量引用的对象的属性。这样,您已经阅读了修改权限,可以这么说。

一个简短的例子来解释你的代码中发生了什么:

//so we create an object
var foo = {}
  , bar;

//and assign a baz property carrying bam
foo.baz = 'bam';

//we reference foo.baz with bar
bar = foo.baz;

//so we expect that bar is bam
console.log(bar); //bam

//however, in this operation, we merely assign a value boom to bar
//and not modify foo.baz
bar = 'boom';

//with bar modified in that way, foo.baz remains as bam
console.log(bar); //boom
console.log(foo.baz) //bam?

相反,here's a simplified approach带有代码的剥离版本:

var List = function () {
    this.head = {}
};
List.prototype = {
    insert: function (element) {
        var node = this.head;
        while (node.next) {
            node = node.next
        }
        node.next = {
            "element": element
        };
    },
    remove: function (element) {

        //so we start with head
        var node = this.head;

        //basically, we assign node as the next node
        //that way, we'll be operating on next instead of node
        //so we can modify the values

        //while the next isn't the one we are after
        while (node.next.element != element) {
            //point to the next
            node = node.next;
        }

        //when we get our target, delete it
        delete node.next.element;

        //we don't assign to the variable node, but to a property
        //of the object node is referencing to
        node.next = node.next.next;
    }
};

另外,请将您的变量,属性和内容详细命名。