前置函数中的对象类型错误

时间:2016-06-29 02:56:14

标签: javascript list data-structures prepend

我目前正在从Eloquent JS书中学习Javascript和DataStructures。我当前的作业是"编写辅助函数prepend,它接受一个元素和一个列表,并创建一个新的列表,将该元素添加到inoput列表的前面"。我想改进,所以我不一定要找我的工作为我完成,但请开导我。我有3个问题,但我的主要问题是第1个问题:

1:在我的代码下面,我的类型错误在哪里?控制台日志说" TypeError:oldObj未定义"但我在函数内初始化它。

lsPre = function(obj)
{   
    var iter = 0;
    var maxIT = 0; //use to capture final     object iteration
    var oldObj = obj; //initialize old object

    //create new object to prepend to old object
    var newObj = {iteration: iter,
    value: 'Overwrite this Value',
              next: oldObj.next
             };

    while (oldObj.iteration + 1 > maxIT)
    {
        maxIT++;
        newObj.next = oldObj.next;
        oldObj = oldObj.next;
    }
    return newObj;
}

2:是否有更优化的解决方案(范例),然后我的预设功能?我试图学习更多面向js的范式,并且暂时避免使用递归解决方案。

3:下面是与上述相同问题集中的早期问题的链接列表递归解决方案。是否有理由将对象返回为:

Object {iteration: 1, value: 2, next: Object}

而不是:

Object {iteration: 1, value: x, next: Object { iteration: 2, value: x, next: (and so on) }}

以下是代码:

//Link List
rayREC = function(ray,iter=0)
{   //Default param 'inc' allows arbitrary implementation of this function
    var rLen = ray.length;
    if (iter > rLen) return;
    return  {//recurively increment objects
             iteration: iter,
             value: ray[iter],
             next: rayREC(ray,iter + 1), //recursion
           };
}

我也想要3的最佳范例或解决方案。

1 个答案:

答案 0 :(得分:0)

回答1:

您收到"TypeError: oldObj is undefined"可能是因为您已使用undefined参数调用该函数。

E.g:

lsPre(); // "TypeError: oldObj is undefined"

lsPre({iteration: 1, value: 2, next: Object}) // Works... well, kind of...

回答2:

这取决于您正在实施的链接列表的类型。

例如,一个非常简单的链接列表valuenext

var simpleListedList = {"value": "Value One", "next": null};

你可以看到这个所谓的"链接列表"是一个独立的节点。

To" prepend&#34 ;,即在前面添加一些东西,我们可以简单地说:

var oldNode = simpleListedList;
simpleListedList = {"value": "Value Zero (New)", "next": oldNode};

我们也可以像这样无脑地迭代它:

var currentNode = simpleListedList;
while (currentNode != null){
    console.log(currentNode.value);
    currentNode = currentNode.next;
}

使用" next-based"链表,它实际上更难追加(即在后面添加):

// First iterate to the last node
var lastNode = simpleListedList;
while (lastNode.next != null) {
    lastNode = lastNode.next;
}

// We then add something at the end
lastNode.next = {"value": "Value N plus One (New last guy)", "next": null}

// Note that simpleListedList is still the first node, unchanged.

回答3:

我真的不明白这个问题。

Object {iteration: 1, value: 2, next: Object}

只有一个节点,next值是一个常量对象(不能迭代)

Object {iteration: 1, value: x, next: Object { iteration: 2, value: x, next: (and so on) }}

这似乎是一个正确的链表,如果你用JSON表示它,它应该是这样的:

{
    iteration: 1,
    value: "val of 1",
    next: {
        iteration: 2,
        value: "val of 2",
        next: {
            iteration: 3,
            value: "val of 3",
            next: {
                iteration: 4,
                value: "val of 4",
                next: {
                    // ... Not sure how you would terminate the linked list
                }
            }
        }
    }
}