0

我正在尝试在 jquery 中创建一个列表对象,我正在使用以下代码,但它不起作用,我找到了这个示例,所以我写了一个类似的东西。

function Node(left, right, content)
{
    this.left = left;
    this.right = right;
    this.content = content;
}

var n1 = new Node('', n2, 'content content content');    
var n2 = new Node(n1, n2, 'content content content');
var n3 = new Node(n2, '', 'content content content');

console.log(box2);

现在,如果我称之为n1.right返回未定义。请帮帮我。

4

7 回答 7

2

不要使用leftright链接到其他节点,而是将所有节点包含在另一个节点中,该节点将充当列表的容器。

您可以从外部节点访问元素。

例如:

function Node(content) {
  this.nodes = [];
  this.content = content;
  this.addNode = function(node) {
    this.nodes.push(node);
  };
  this.changeContent = function(newContent) {
    this.content = newContent;
  };
}

var container = new Node();
container.addNode(new Node("Node 1"));
container.addNode(new Node("Node 2"));
container.addNode(new Node("Node 3"));

console.log(container);

正如在 Codepen 上看到的

于 2013-01-23T13:52:18.767 回答
1

你在它有值之前传递n2给它,因此它是. 在节点存在之前,您不能将其传递给另一个节点。new Nodeundefined

一个简单的解决方法是在创建节点后“链接”节点:

function Node(content, left, right)
{
    this.left = left;
    this.right = right;
    this.content = content;
}

var n1 = new Node('content content content');    
var n2 = new Node('content content content');
var n3 = new Node('content content content');

n1.right = n2;
n2.left = n1;
n2.right = n3;
n3.left = n2;

尽管您可能需要一些 getter 和 setter 方法来正确处理将新节点插入到列表中。

了解有关对象如何工作的更多信息:MDN - Woking with objects


不过,这可能是一个 XY 问题,在这种情况下,您必须解释您实际尝试解决的问题。如果您只想创建对象列表,可以使用数组[MDN]

于 2013-01-23T13:57:26.423 回答
0

看看你的代码:

var n1 = new Node('', n2, 'content content content');    
var n2 = new Node(n1, n2, 'content content content');
var n3 = new Node(n2, '', 'content content content');

创建n1时没有n2对象,因此插入undefined.

于 2013-01-23T13:54:18.933 回答
0

首先,您在这里省略了一些有用的代码。例如 box2 对象是什么。

无论如何,您尝试将 n2 添加到 n1 但它尚不存在:

var n1 = new Node('', n2, 'content content content'); // n2 does not yet exist   
var n2 = new Node(n1, n2, 'content content content'); // n2 can't reference itself yet
var n3 = new Node(n2, '', 'content content content');

您应该首先创建一个根节点或创建一个更复杂的对象模式,您可以在创建节点后将连接添加到这些节点。

于 2013-01-23T13:54:31.843 回答
0
function Node(left, right, content)
{
    this.left = left;
    this.right = right;
    this.content = content;
}

var n1 = new Node('', null, 'content content content');    
var n2 = new Node(n1, n2, 'content content content');

n1.right=n2;

var n3 = new Node(n2, '', 'content content content');

console.log(n1.right);
于 2013-01-23T13:56:43.507 回答
0

您可以让 Node 构造函数更新左/右节点。因此,如果当前节点的左侧为 n2,则 n2 的右侧应为当前节点:

function Node(left, right, content)
{
    this.left = left;  
    this.right = right;
    this.content = content;
    if(left) {
       left.right = this;
    }
    if(right) {
       right.left = this;
    }
}

var n1 = new Node(null, null, 'content content content');    
var n2 = new Node(n1, null, 'content content content');
var n3 = new Node(n2, null, 'content content content');

console.log(n1.right.content);
于 2013-01-23T14:00:24.037 回答
0

就像其他人说的那样,在创建之前您不能分配价值。你可以做一些延迟加载,但这听起来会让你的事情变得复杂。试试这个:

function Node(content, prev, next) {
    var setPrev = function(node){
        var oldPrev = this.prev;
        this.prev = node;
        node.next = this;
        node.prev = oldPrev;
    }
    ,setNext = function(node){
        var oldNext = this.next;
        this.next = node;
        node.next = oldNext;
        node.prev = this;
    }
    return {
        'content': content,
        'prev': prev,
        'next': next,
        'setPrev': setPrev,
        'setNext': setNext
    }
}
n1 = new Node('content content content');    
n2 = new Node('content content content', n1);
n3 = new Node('content content content', n2);
n1.setNext(n2);
n2.setNext(n3);

将它封装到某种可以处理诸如appendNode, prependNode, insertAfter,insertBefore等的 List 对象中也是一个好主意。

于 2013-01-23T14:03:28.913 回答