JS 外作用域变量
JS Outer scoped variable
我一直在断断续续地开发一款基于游戏节目倒计时的游戏应用程序。到目前为止,我已经能够让用户选择元音和辅音并输入一个单词,检查输入的单词是否包含生成的相同字母。我创建了一个匿名函数,它使用 jQuery 检查字典文本文件以查看它是否已列出。但是我 运行 遇到了麻烦,我不断收到警告消息 "functions declared within loops referencing an outer scoped variable may lead to confusing semantics."
我不确定我做错了什么,请看下面的代码:
$(".submission").hide();
//assigning letter container class to variable
var letterContainer = document.querySelector(".letter-container");
function pickFrom(letters) {
// if the length of the letter container is less than 8
if (letterContainer.innerHTML.length < 8) {
//add letters to the letter container
letterContainer.innerHTML += rando(letters);
}
//once letter container length reaches 8
if (letterContainer.innerHTML.length === 8) {
//show submission input and button
$(".submission").show();
//remove letter buttons after 8 letters have been chosen by user
$(".letter-btn").hide();
//assign given letters from letter container to a variable
var givenLetters = letterContainer.innerHTML.split("");
var inputWord = document.querySelector(".enter-word").value;
document.querySelector(".submit-word").addEventListener("click", function() {
var inputWord = document.querySelector(".enter-word").value.split("");
if (inputWord.length <= 8) {
//
var isValid;
for (var i = 0; i < inputWord.length; i++) {
// console.log(inputWord[i]);
if (givenLetters.includes(inputWord[i])) {
var indexPosition = givenLetters.indexOf(inputWord[i]);
givenLetters.splice(indexPosition, 1);
var newWord = inputWord.join("").toLowerCase();
$.get("https://raw.githubusercontent.com/cpog19901/Countdown/master/text/english3.txt", function(contents) {
//contents variable now contains the contents of the textfile as string
//check if file contains the word entered by user
var hasString = contents.includes(newWord);
//outputs true if contained, else false
console.log(hasString);
});
isValid = true;
} else {
console.log(inputWord[i]);
console.log("no");
isValid = false;
}
}
if (isValid == true) {
console.log("This is a valid word");
}
else if (isValid == false) {
console.log("This is NOT a valid word");
}
}
});
}
}
我需要所有的陈述都是真实的,包括匿名函数,这样它会让用户知道它是一个有效的词。
错误消息表明要注意 for
循环。我找到了这个:
for (var i = 0; i < inputWord.length; i++) {
/* … */
$.get("https://raw.githubusercontent.com/cpog19901/Countdown/master/text/english3.txt", function(contents) {
/* … */
}
/* … */
}
此 AJAX 调用在循环的每一次迭代 中进行(假设周围的 if
为真)。
相反,您可以更早地执行此操作并将结果存储在变量中。
我一直在断断续续地开发一款基于游戏节目倒计时的游戏应用程序。到目前为止,我已经能够让用户选择元音和辅音并输入一个单词,检查输入的单词是否包含生成的相同字母。我创建了一个匿名函数,它使用 jQuery 检查字典文本文件以查看它是否已列出。但是我 运行 遇到了麻烦,我不断收到警告消息 "functions declared within loops referencing an outer scoped variable may lead to confusing semantics."
我不确定我做错了什么,请看下面的代码:
$(".submission").hide();
//assigning letter container class to variable
var letterContainer = document.querySelector(".letter-container");
function pickFrom(letters) {
// if the length of the letter container is less than 8
if (letterContainer.innerHTML.length < 8) {
//add letters to the letter container
letterContainer.innerHTML += rando(letters);
}
//once letter container length reaches 8
if (letterContainer.innerHTML.length === 8) {
//show submission input and button
$(".submission").show();
//remove letter buttons after 8 letters have been chosen by user
$(".letter-btn").hide();
//assign given letters from letter container to a variable
var givenLetters = letterContainer.innerHTML.split("");
var inputWord = document.querySelector(".enter-word").value;
document.querySelector(".submit-word").addEventListener("click", function() {
var inputWord = document.querySelector(".enter-word").value.split("");
if (inputWord.length <= 8) {
//
var isValid;
for (var i = 0; i < inputWord.length; i++) {
// console.log(inputWord[i]);
if (givenLetters.includes(inputWord[i])) {
var indexPosition = givenLetters.indexOf(inputWord[i]);
givenLetters.splice(indexPosition, 1);
var newWord = inputWord.join("").toLowerCase();
$.get("https://raw.githubusercontent.com/cpog19901/Countdown/master/text/english3.txt", function(contents) {
//contents variable now contains the contents of the textfile as string
//check if file contains the word entered by user
var hasString = contents.includes(newWord);
//outputs true if contained, else false
console.log(hasString);
});
isValid = true;
} else {
console.log(inputWord[i]);
console.log("no");
isValid = false;
}
}
if (isValid == true) {
console.log("This is a valid word");
}
else if (isValid == false) {
console.log("This is NOT a valid word");
}
}
});
}
}
我需要所有的陈述都是真实的,包括匿名函数,这样它会让用户知道它是一个有效的词。
错误消息表明要注意 for
循环。我找到了这个:
for (var i = 0; i < inputWord.length; i++) {
/* … */
$.get("https://raw.githubusercontent.com/cpog19901/Countdown/master/text/english3.txt", function(contents) {
/* … */
}
/* … */
}
此 AJAX 调用在循环的每一次迭代 中进行(假设周围的 if
为真)。
相反,您可以更早地执行此操作并将结果存储在变量中。