javascript小白,问一个代码问题,卡住了

constcomputerList=["algorithm","alignment","backup","alpha","analog","bandwidth","bin... const computerList = ["algorithm", "alignment", "backup", "alpha", "analog", "bandwidth", "binary", "boolean", "bug", "cache","capacity", "cell", "client", "code", "database", "debug", "desktop publishing", "encryption", "external storage", "feedback", "firewall", "gigabyte", "hyperlink", "import", "interface", "local area network", "microprocessor", "operating system", "peripheral", "plagiarism"];
const medicalList = ["abnormal", "ache", "ambulance", "biopsy", "blood count", "cancer", "cast", "chemotherapy", "deaf", "discomfort", "fatal", "germ", "genetic", "HIV", "immune system", "infant", "itchy", "light-headed", "malignant", "newborn", "numb", "pain", "paralyzed", "scrubs", "side effects", "therapy", "unconscious", "vein", "ward", "wound"];
const carList = ["Toyota", "Alfa Romeo", "Audi", "Volkswagen", "BMW", "Subaru", "Cadillac", "Chevrolet", "Citroen", "Ferrari", "Ford", "Geely", "GMC", "Honda", "Hyundai", "Infiniti", "Jaguar", "Jeep", "Kia", "Volvo", "Land Rover", "Lexus", "Maserati", "Mazda", "Mercedes-Benz", "Mini", "Peugeot", "Porsche", "Renault", "Rolls Royce"];
//以上是三个数组
var wordLists = ​select.addEventListener('change', selection);
//这里我把返回的数组给了wordlists了
function selection()
{
var wordList=""; displayWords.innerHTML = ""; input.value = ""; time.innerHTML = "";
result.innerHTML = "";
button.disabled = false;
if(event.value="1")
{ wordList = computerList; }
else if(event.value="2")
{ wordList = medicalList; }
else if(event.value="3")
{ wordList = carList; }
return wordList;//返回数组
}

button.addEventListener('click', startGame);
//按了button运行下面的方法,方法能进去,但是为什么返回的数组进不去呢
function startGame()
{
var randomNumber = Math.round(Math.random()*wordList.length);
button.disabled = true;
displayWords.innerHTML=wordList[randomNumber];
displayWords.style.backgroundColor="lightblue";
displayWords.style.borderWidth="5px";
displayWords.style.borderColor="cornflowerblue";
}
为什么startGame()方法里面用不了wordList这个数组呢?
展开
 我来答
海南岛也会下雪
2019-07-13 · TA获得超过317个赞
知道小有建树答主
回答量:312
采纳率:74%
帮助的人:69.6万
展开全部
因为wordList数组 是在函数selection 内部定义的,他的作用域就是那个函数里面.别的地方无法使用的.你可以在最外层定义他
更多追问追答
追问
谢谢,但是换到外面之后,if里面的条件不好用了,wordList被赋予值一次后第二次没法更新他的值了
追答

重复定义了。 数组本是就是引用地址类型的。 不用写返回。 直接在事件里给外面的数组赋值就行了

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式