当我调用.length时,为什么这个数组是未定义的?

时间:2022-07-02 21:43:02

I am working on an exercise from the book Eloquent JavaScript (see 'a list' at bottom of linked page). Basically, I want to make an object that takes an array and turns it into a list with the following structure:

我正在从Eloquent JavaScript这本书开始练习(参见链接页面底部的'列表')。基本上,我想创建一个带有数组的对象,并将其转换为具有以下结构的列表:

var list = {
  value: 1,
  rest: {
    value: 2,
    rest: {
      value: 3,
      rest: null
    }
  }
};

When I run the code below in the console, I get TypeError: Cannot read property 'length' of undefined (line 3 in function arrayToList). Why is the array on which I am calling .length not defined?

当我在控制台中运行下面的代码时,我得到TypeError:无法读取undefined的属性'length'(函数arrayToList中的第3行)。为什么我调用的数组.length没有定义?

function arrayToList(array){
    var list = {};
    if(!array.length){
        list.value = null;
    }
    else{
        list.value = array.shift();
        list.rest = arrayToList();
    }
    return list;
}

console.log(arrayToList([10, 20]));

Note: Stack Overflow question List data structures in JavaScript is a very similar post on the same problem. I'm not looking for a solution here so much as an explanation as to what is going haywire in the recursive call.

注意:Stack Overflow问题JavaScript中的列表数据结构是关于同一问题的非常相似的帖子。我不是在寻找解决方案,而是在递归调用中解释什么是乱七八糟的。

4 个解决方案

#1


12  

list.rest = arrayToList();

Since you don't pass any parameter to arrayToList, array will have the default value undefined. That is why it is failing with

由于未将任何参数传递给arrayToList,因此数组的默认值为undefined。这就是它失败的原因

TypeError: Cannot read property 'length' of undefined

TypeError:无法读取未定义的属性“length”

So, during the recursive call, you are supposed to pass an array to arrayToList, again.

因此,在递归调用期间,您应该再次将数组传递给arrayToList。

function arrayToList(array) {
    var list = {};
    if (!array.length) {
        list.value = null;
    } else {
        list.value = array.shift();
        list.rest = arrayToList(array);   // Pass the `array` after shifting
    }
    return list;
}

console.log(arrayToList([10, 20]));
# { value: 10, rest: { value: 20, rest: { value: null } } }

#2


2  

On this line:

在这一行:

    list.rest = arrayToList();

… you recursively call the function but you don't specify an argument, so array becomes undefined.

...您递归调用函数但未指定参数,因此数组变为未定义。

It looks like you should just pass array again (which you have modified).

看起来你应该再次传递数组(你已修改过)。

    list.rest = arrayToList(array);

live demo

现场演示

#3


2  

In the recursive arrayToList call, you're not passing anything as a parameter (!).

在递归的arrayToList调用中,您不会将任何内容作为参数传递(!)。

#4


0  

function arrayToList(array) {
    var list = {};
    if (!(array instanceof Array) || array.length == 0) {
       list.value = null;
    } else {
        list.value = array.shift();
        list.rest = arrayToList();
    }
    return list;
}

#1


12  

list.rest = arrayToList();

Since you don't pass any parameter to arrayToList, array will have the default value undefined. That is why it is failing with

由于未将任何参数传递给arrayToList,因此数组的默认值为undefined。这就是它失败的原因

TypeError: Cannot read property 'length' of undefined

TypeError:无法读取未定义的属性“length”

So, during the recursive call, you are supposed to pass an array to arrayToList, again.

因此,在递归调用期间,您应该再次将数组传递给arrayToList。

function arrayToList(array) {
    var list = {};
    if (!array.length) {
        list.value = null;
    } else {
        list.value = array.shift();
        list.rest = arrayToList(array);   // Pass the `array` after shifting
    }
    return list;
}

console.log(arrayToList([10, 20]));
# { value: 10, rest: { value: 20, rest: { value: null } } }

#2


2  

On this line:

在这一行:

    list.rest = arrayToList();

… you recursively call the function but you don't specify an argument, so array becomes undefined.

...您递归调用函数但未指定参数,因此数组变为未定义。

It looks like you should just pass array again (which you have modified).

看起来你应该再次传递数组(你已修改过)。

    list.rest = arrayToList(array);

live demo

现场演示

#3


2  

In the recursive arrayToList call, you're not passing anything as a parameter (!).

在递归的arrayToList调用中,您不会将任何内容作为参数传递(!)。

#4


0  

function arrayToList(array) {
    var list = {};
    if (!(array instanceof Array) || array.length == 0) {
       list.value = null;
    } else {
        list.value = array.shift();
        list.rest = arrayToList();
    }
    return list;
}