为什么函数结束后我的数组变得未定义?

FireyDeath4-YuAre一个陌生人

好的,我只是在做一个将所有内容从一个列表移到另一个列表的函数,完成后,它会自发地变得不确定。这样做没有充分的理由,但是不好的原因是什么呢?

来看

function moveList(){//in my real program it actually shuffles the list, this one just transfers the contents
    var listOne=[0, 1, 2, 3];//we have a list
    var listTwo=[];//now we have an empty list
    var length = listOne.length//and now we have the list length
    for (run = 0; run < length; run++) {
        listTwo.push(listOne[0]);//it just copies the first entry from the first list to the second
        listOne.splice(0, 1,)//and deletes it from the first list
    }//this does it for an entry in the list, it's length amount of times
    console.log("listTwo: " + listTwo);//this tells us what the new list now is, and it works
    console.log("listOne: " + listOne);//this tells us what the original list is, which is empty
}
var listOne;
var listTwo;
//these are mandatory, without this, even with the function below script,
//you have Uncaught ReferenceError: list(One and Two) is not defined
moveList();//now we do the function, and then,
console.log("listTwo: " + listTwo);//THE LIST IS NOW UNDEFINED??? What??????

只是为什么 我只是在我的函数中定义了它。你怎么了(从字面上看,我没有看到)?

艾哈迈德·哈默德|

原因是您用listTwo不同范围的名称初始化了两个列表全局作用域中的一个变量,您不给它赋值,因此它停留在undefined,而另一个变量在函数内部,并且不能从函数外部访问,因为已经有另一个具有相同名称的变量!

为了使代码正常工作,您应该在函数内部使用变量而不声明它们。

function moveList(){//in my real program it actually shuffles the list, this one just transfers the contents
    listOne=[0, 1, 2, 3];//we have a list
    listTwo=[];//now we have an empty list
    var length = listOne.length//and now we have the list length
    for (run = 0; run < length; run++) {
        listTwo.push(listOne[0]);//it just copies the first entry from the first list to the second
        listOne.splice(0, 1,)//and deletes it from the first list
    }//this does it for an entry in the list, it's length amount of times
    console.log("listTwo: " + listTwo);//this tells us what the new list now is, and it works
    console.log("listOne: " + listOne);//this tells us what the original list is, which is empty
}
var listOne;
var listTwo;

moveList();//now we do the function, and then,
console.log("listTwo: " + listTwo);//THE LIST IS NOW NOT UNDEFINED ;)

同样适用于listOne变量。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我变得未定义不是函数,ReactJs

来自分类Dev

为什么我的MySQL函数不断变得“未定义”?

来自分类Dev

为什么返回值在我的递归函数中变得未定义?

来自分类Dev

为什么我的变量在更改另一个变量的显示后变得未定义?

来自分类Dev

为什么我的变量在我的函数之外未定义?

来自分类Dev

在我进行控制台操作后,数组变得未定义。

来自分类Dev

为什么我的函数返回一个未定义的数组作为项目?

来自分类Dev

为什么订阅后我的对象的变量未定义

来自分类Dev

删除项目后状态数组变得未定义

来自分类Dev

为什么我得到未定义的构造函数

来自分类Dev

为什么我的函数未定义且未加载

来自分类Dev

为什么在调用我的函数时未定义它?

来自分类Dev

为什么我的递归函数返回未定义?

来自分类Dev

为什么我得到未定义的不是函数?

来自分类Dev

为什么我的反应通量函数未定义?

来自分类Dev

为什么我的 javascript 抛出函数未定义错误?

来自分类Dev

为什么我的递归函数返回“未定义”?

来自分类Dev

为什么未定义我的公共静态数组?

来自分类Dev

为什么我将数组值设为“未定义”

来自分类Dev

为什么变量在$(document).keypress中变得未定义?

来自分类Dev

为什么变量在$ {document).keypress中变得未定义?

来自分类Dev

为什么这个jQuery OOP属性变得未定义?

来自分类Dev

为什么在两次请求后我的数组未定义?(使用 chart.js 和 stock.js)

来自分类Dev

为什么函数参数未定义?

来自分类Dev

调用未定义的函数,为什么?

来自分类Dev

为什么函数返回未定义?

来自分类Dev

为什么未定义此数组?

来自分类Dev

为什么我的输出未定义

来自分类Dev

为什么我的变量“未定义”

Related 相关文章

  1. 1

    为什么我变得未定义不是函数,ReactJs

  2. 2

    为什么我的MySQL函数不断变得“未定义”?

  3. 3

    为什么返回值在我的递归函数中变得未定义?

  4. 4

    为什么我的变量在更改另一个变量的显示后变得未定义?

  5. 5

    为什么我的变量在我的函数之外未定义?

  6. 6

    在我进行控制台操作后,数组变得未定义。

  7. 7

    为什么我的函数返回一个未定义的数组作为项目?

  8. 8

    为什么订阅后我的对象的变量未定义

  9. 9

    删除项目后状态数组变得未定义

  10. 10

    为什么我得到未定义的构造函数

  11. 11

    为什么我的函数未定义且未加载

  12. 12

    为什么在调用我的函数时未定义它?

  13. 13

    为什么我的递归函数返回未定义?

  14. 14

    为什么我得到未定义的不是函数?

  15. 15

    为什么我的反应通量函数未定义?

  16. 16

    为什么我的 javascript 抛出函数未定义错误?

  17. 17

    为什么我的递归函数返回“未定义”?

  18. 18

    为什么未定义我的公共静态数组?

  19. 19

    为什么我将数组值设为“未定义”

  20. 20

    为什么变量在$(document).keypress中变得未定义?

  21. 21

    为什么变量在$ {document).keypress中变得未定义?

  22. 22

    为什么这个jQuery OOP属性变得未定义?

  23. 23

    为什么在两次请求后我的数组未定义?(使用 chart.js 和 stock.js)

  24. 24

    为什么函数参数未定义?

  25. 25

    调用未定义的函数,为什么?

  26. 26

    为什么函数返回未定义?

  27. 27

    为什么未定义此数组?

  28. 28

    为什么我的输出未定义

  29. 29

    为什么我的变量“未定义”

热门标签

归档