Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么函数一结束我的数组就没有定义?_Javascript_Arrays_Function - Fatal编程技术网

Javascript 为什么函数一结束我的数组就没有定义?

Javascript 为什么函数一结束我的数组就没有定义?,javascript,arrays,function,Javascript,Arrays,Function,好的,我只是在做一个函数,将所有内容从一个列表移动到另一个列表,一旦完成,它就会自动变得未定义。它这样做没有好的理由,但坏的理由是什么 来,看 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=[];//no

好的,我只是在做一个函数,将所有内容从一个列表移动到另一个列表,一旦完成,它就会自动变得未定义。它这样做没有好的理由,但坏的理由是什么

来,看

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??????
function moveList(){//在我的实际程序中,它实际上是对列表进行洗牌,而这个只是传输内容
var listOne=[0,1,2,3];//我们有一个列表
var listwo=[];//现在我们有了一个空列表
var length=listOne.length//现在我们有了列表长度
对于(运行=0;运行<长度;运行++){
push(listOne[0]);//它只是将第一个条目从第一个列表复制到第二个列表
splice(0,1,)//并将其从第一个列表中删除
}//对于列表中的一个条目,它的长度为
log(“listTwo:+listTwo);//这告诉我们新列表现在是什么,它可以工作
log(“listOne:+listOne);//这告诉我们原始列表是什么,它是空的
}
var-listOne;
var列表二;
//这些都是必需的,没有这些,即使使用下面的脚本函数,
//您有未捕获的引用错误:未定义列表(一和二)
移动列表()//现在我们做这个函数,然后,
log(“listTwo:+listTwo”)//列表现在未定义???什么??????

为什么?我只是在函数中定义了它。你怎么了(字面意思,我不明白)?

你有两个独立的
列表两个
变量。一个是全局的,而另一个是函数
moveList
的本地的。这两件事彼此无关。这同样适用于
listOne

因此,您的代码相当于下面的代码,我已将名称更改为更清晰:

function moveList(){//在我的实际程序中,它实际上是对列表进行洗牌,而这个只是传输内容
var localListOne=[0,1,2,3];//我们有一个列表
var locallistwo=[];//现在我们有了一个空列表
var length=localListOne.length//现在我们有了列表长度
对于(运行=0;运行<长度;运行++){
push(localListOne[0]);//它只是将第一个条目从第一个列表复制到第二个列表
splice(0,1,)//并将其从第一个列表中删除
}//对于列表中的一个条目,它的长度为
log(“listTwo:+localListTwo);//这告诉我们新列表现在是什么,并且它可以工作
log(“listOne:+localListOne);//这告诉我们原始列表是什么,它是空的
}
球粒石;
var globallist2;
//这些都是必需的,没有这些,即使使用下面的脚本函数,
//您有未捕获的引用错误:未定义列表(一和二)
移动列表()//现在我们做这个函数,然后,
log(“listTwo:+globalListTwo);//显然,globalListTwo从未被赋予值,因此它是未定义的。

您有两个独立的
列表和两个
变量。一个是全局的,而另一个是函数
moveList
的本地的。这两件事彼此无关。这同样适用于
listOne

因此,您的代码相当于下面的代码,我已将名称更改为更清晰:

function moveList(){//在我的实际程序中,它实际上是对列表进行洗牌,而这个只是传输内容
var localListOne=[0,1,2,3];//我们有一个列表
var locallistwo=[];//现在我们有了一个空列表
var length=localListOne.length//现在我们有了列表长度
对于(运行=0;运行<长度;运行++){
push(localListOne[0]);//它只是将第一个条目从第一个列表复制到第二个列表
splice(0,1,)//并将其从第一个列表中删除
}//对于列表中的一个条目,它的长度为
log(“listTwo:+localListTwo);//这告诉我们新列表现在是什么,并且它可以工作
log(“listOne:+localListOne);//这告诉我们原始列表是什么,它是空的
}
球粒石;
var globallist2;
//这些都是必需的,没有这些,即使使用下面的脚本函数,
//您有未捕获的引用错误:未定义列表(一和二)
移动列表()//现在我们做这个函数,然后,
log(“listTwo:+globalListTwo);//显然,globalListTwo从未被赋予值,因此它是未定义的。

原因是您在不同的范围内初始化了两个名为
listwo
的列表!一个在全局范围内,您没有给它一个值,因此它保持
未定义
,另一个在函数内部,并且无法从函数外部访问,因为已经有另一个同名变量

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

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 ;)
function moveList(){//在我的实际程序中,它实际上是对列表进行洗牌,而这个只是传输内容
listOne=[0,1,2,3];//我们有一个列表
listTwo=[];//现在我们有了一个空列表
var length=listOne.length//现在我们有了列表长度
对于(运行=0;运行<长度;运行++){
push(listOne[0]);//它只是将第一个条目从第一个列表复制到第二个列表
splice(0,1,)//并将其从第一个列表中删除
}//对于列表中的一个条目,它的长度为
log(“listTwo:+listTwo);//这告诉我们新列表现在是什么,它可以工作
log(“listOne:+listOne);//这告诉我们原始列表是什么,它是空的
}
var-listOne;
var列表二;
移动列表()//现在我们做这个函数,然后,
log(“listTwo:+listTwo”)//列表现在不是未定义的;)

这同样适用于
listOne
变量。

原因是您在不同的范围内初始化了两个名为
listwo
的列表!一个在全局范围内,你没有给它一个值,所以它保持