[英]All variables have global scope but functions still can't access them
我已經在我的代碼中將所有變量設為全局變量,但我的 function 仍然無法訪問它們,我相信這也會導致我的代碼中的函數像 getValueOfEasy() function 一樣未定義(是否還有另一個原因導致我的函數出現為定義時“未定義”?)
感謝任何幫助,我的主要問題是我無法弄清楚為什么函數無法讀取我的變量。
我的代碼鏈接在這里: http://jsfiddle.net/6s8y4p9k/6/
代碼:
<html>
<script>
function getValueOfEasy(){
var easy = document.getElementById("easy").value;
}
function getValueOfMedium(){
var medium = document.getElementById("medium").value;
}
function getValueOfHard(){
var hard = document.getElementById("hard").value;
}
<!--gives number to sentence -->
var FirstE2;
var FirstM2;
var FirstH2;
var SecondE2;
var SecondM2;
var SecondH2;
var ThirdE2;
var ThirdM2;
var ThirdH2;
<!-- prints __ var sentce to schedule -->
var FirstE;
var FirstM;
var FirstH;
var SecondE;
var SecondM;
var SecondH;
var ThirdE;
var ThirdM;
var ThirdH;
function timeE() {
if (easy < 4){
FirstE = "do your # easy assignemnts first";
SecondE2 = 3;
}else if (easy === 3) {
SecondE = "do your # easy assignments second";
SecondE2 = 3;
} else if (easy <= 2) {
ThirdE = "Do your # easy assignemnts last ";
ThirdE2 = 3;
}
}
function timeM() {
if (medium >= 3) {
FirstM = "do your # medium assignments first";
FirstM2 = 2;
} else if (medium === 2) {
SecondM = "do your # medium assignments second"
SecondM2 = 2;
} else if (medium === 1) {
ThirdM = "Do your # medium assignments last"
ThirdM2 = 2;
}
}
function timeH() {
if (hard >= 1) {
FirstH = "do your # hard assignments first";
FirstH2 = 1;
} else {
return;
}
var excitedE;
var excitedM;
var excitedH;
function yesE() {
excitedE = 3;
}
function mehE() {
excitedE = 2;
}
function noE() {
excitedE = 1;
}
function yesM {
excitedM = 3;
}
function mehM() {
excitedM = 2;
}
function noM() {
excitedM = 1;
}
function yesH {
excitedH = 3;
}
function mehH() {
excitedH = 2;
}
function noH() {
excitedH = 1;
}
function ExcitedE() {
if (excitedE = 3 && FirstE2 = 3) {
FirstE = "do your # easy assignments last";
} else if (excitedE = 3 && FirstE2 = 3 && easy >= 4) {
ThirdE = "do your # easy assignments third";
}
//easy: 1st of list & yes excited
else if (excitedE = 2 && FirstE2 = 3) {
SecondE = "do your # easy assignments Second";
}
//easy: 1st of list & maybe excited
else if (excitedE = 1 && FirstE2 = 3) {
FirstE = "do your # easy assignments first";
}
//easy: 1st of list & not excited
else if (excitedE = 3 && SecondE2 = 3) {
SecondE = "do your # easy assignments second";
}
//easy: 2nd of list & yes excited
else if (excitedE = 2 && SecondE2 = 3) {
SecondE = "do your # easy assignments second";
}
//easy: 2nd of list and maybe excited
else if (excitedE = 1 && SecondE2 = 3) {
FirstE = "do your # easy assignments first";
}
//easy: 2nd and no
if (excitedE = 3 && ThirdE2 = 3) {
ThirdE = "do your # easy assignments third";
}
//easy: 3rd of list & yes excited
else if (excitedE = 3 && ThirdE2 = 3) {
SecondE = "do your # easy assignments second";
}
//easy: 3nd of list and maybe excited
else if (excitedE = 2 && ThirdE2 = 3) {
FirstE = "do your # easy assignments first";
}
//easy: 3rd and no
else if (excitedE = 1 && ThirdE2 = 3) {
FirstE = "do your # easy assignments first";
}
}
function ExcitedM() {
if (excitedM = 3 && FirstM2 = 3 && medium >= 2) {
ThirdM = "do your # easy assignments third";
} else if (excitedM = 3 && FirstE2 = 3) {
FirstM = "do your # easy assignments last";
}
//medium: 1st of list & yes excited
if (excitedM = 3 && SecondM2 = 3) {
SecondM = "do your # easy assignments second";
}
//medium: 2nd of list & yes excited
else if (excitedM = 2 && SecondM2 = 3) {
SecondM = "do your # easy assignments second";
}
//medium: 2nd of list and maybe excited
else if (excitedM = 1 && SecondM2 = 3) {
FirstM = "do your # easy assignments first";
}
//medium: 2nd and no
if (excitedM = 3 && ThirdM2 = 3) {
ThirdM = "do your # easy assignments third";
}
//medium: 3rd of list & yes excited
else if (excitedM = 2 && ThirdM2 = 3) {
SecondM = "do your # easy assignments second";
}
//medium: 3nd of list and maybe excited
else if (excitedM = 3 && ThirdM2 = 3) {
FirstM = "do your # easy assignments first";
}
//medium: 3rd and no
} else {
return;
}
function ExcitedH() {
if (excitedE = 3 && FirstH2 = 3 && hard === 1) {
ThirdH = "do your # hard assignments last";
} else if (excitedE = 3 && FirstH2 = 3) {
FirstH = "do your # hard assignments first";
}
//hard: 1st of list & yes excited
if (excitedE = 3 && SecondH2 = 3) {
SecondH = "do your # hard assignments second";
}
//hard: 2nd of list & yes excited
else if (excitedE = 2 && SecondH2 = 3) {
SecondH = "do your # hard assignments second";
}
//hard: 2nd of list and maybe excited
else if (excitedE = 1 && SecondH2 = 3) {
FirstH = "do your # hard assignments first";
}
//hard: 2nd and no
if (excitedE = 3 && ThirdH2 = 3) {
ThirdH = "do your # hard assignments third";
}
//hard: 3rd of list & yes excited
else if (excitedE = 2 && ThirdH2 = 3) {
SecondH = "do your # hard assignments second";
}
//hard: 3nd of list and maybe excited
else if (excitedE = 3 && ThirdH2 = 3) {
FirstH = "do your # hard assignments first";
}
//hard: 3rd and no
} else {
return;
}
function showSchedule() {
document.getElementById("HD1").style.display = "none";
document.getElementById("homeworkSchedule").style.display = "block";
}
function showEnterBtn() {
document.getElementById("save").style.display = "none";
document.getElementById("enter").style.display = "block";
}
function showHard() {
document.getElementById("MD1").style.display = "none";
document.getElementById("HD1").style.display = "block";
}
function showMedium() {
document.getElementById("ED1").style.display = "none";
document.getElementById("MD1").style.display = "block";
}
<
!--makes schedule-- >
function makeHomeworkSchedule() {
document.getElementById("first").innerHTML = FirstE + " " + FirstM + " " + FirstH;
document.getElementById("second").innerHTML = SecondE + " " + SecondM + " " + SecondH;
document.getElementById("third").innerHTML = ThirdE + " " + ThirdM + " "
ThirdH;
}
</script>
<body>
<div id=ED1>
<p>
<center>
Question 1, enter number?
</center>
</p>
<center><input min="0" id="easy" type="number"/></center>
<center>
<p>This an other question, click one of the buttons?</p>
<button class="button button3" onclick="yesE()">Yes</button>
<button class="button button3" onclick="mehE()">Meh</button>
<button class="button button3" onclick="noE()">No</button>
</center>
<h1> </h1>
<center><button class="button button3" onclick="getValueOfEasy(); timeE(); ExcitedE(); showMedium(); " id="easyEnter">Enter</button></center>
</div>
<div id="MD1" style="display:none">
<h1>
Question 2?
</h1>
<input min="0" id="medium" type="text">
<p>this is another question?</p>
<button class="button button3" onclick="yesM()">Yes</button>
<button class="button button3" onclick="mehM()">Meh</button>
<button class="button button3" onclick="noM()">No</button>
<h1> </h1>
<button class="button button3" onclick="getValueOfMedium(); showHard(); timeM(); ExcitedM();" id="mediumEnter">Enter</button>
</div>
<div id="HD1" style="display:none">
<p>
Question 3?
</p>
<input min="0" id="hard" type="text">
<p>WOah another question!?</p>
<button class="button button3" onclick="yesH()">Yes</button>
<button class="button button3" onclick="mehH()">Meh</button>
<button class="button button3" onclick="noH()">No</button>
<h1> </h1>
<button id="save" class="button button3" onclick="getValueOfHard(); timeH(); ExcitedH(); makeHomeworkSchedule();">Save</button>
</div>
<div id="homeworkSchedule" style="display:none">
<p>... paragprah ...</p>
<h1>1st:</h1>
<h5 id="first"></h5>
<h1>2nd:</h1>
<h5 id="second"></h5>
<h1>3rd:</h1>
<h5 id="third"></h5>
</div>
</body>
</html>
我看到的第一件事是錯誤的,你用<!-- comment here -->
評論你的 javascript 代碼,在 javascript 你用/* comment here*/
或// comment here
。
您還使用單個等號=
檢查 if 語句,嘗試在比較時使用==
或===
。
您的代碼中有很多語法錯誤,您應該嘗試使用帶有語法檢查器的代碼編輯器。 在某些情況下,您會忘記關閉方括號或圓括號。
你的函數不是全局的,因為它們是在本地創建的(也就是在另一個函數中),你可以在 function 之外創建函數,然后在 function 中調用這些函數,如下所示:
var excitedE;
function yesE() {
excitedE == 3;
}
function timeH() {
yesE();
}
在 ES6+ 中,我們並沒有真正使用var
來創建變量,您應該對會改變的變量使用let
,對永不改變的變量使用const
。
無論如何,我修復了下面代碼片段中的所有內容。
<html> <script> function getValueOfEasy() { let easy = document.getElementById("easy").value; } function getValueOfMedium() { let medium = document.getElementById("medium").value; } function getValueOfHard() { let hard = document.getElementById("hard").value; } /* gives number to sentence */ let FirstE2; let FirstM2; let FirstH2; let SecondE2; let SecondM2; let SecondH2; let ThirdE2; let ThirdM2; let ThirdH2; /* prints __ let sentence to schedule */ let FirstE; let FirstM; let FirstH; let SecondE; let SecondM; let SecondH; let ThirdE; let ThirdM; let ThirdH; function timeE() { if (easy < 4) { FirstE = "do your # easy assignments first"; SecondE2 = 3; } else if (easy === 3) { SecondE = "do your # easy assignments second"; SecondE2 = 3; } else if (easy <= 2) { ThirdE = "Do your # easy assignments last "; ThirdE2 = 3; } } function timeM() { if (medium >= 3) { FirstM = "do your # medium assignments first"; FirstM2 = 2; } else if (medium === 2) { SecondM = "do your # medium assignments second"; SecondM2 = 2; } else if (medium === 1) { ThirdM = "Do your # medium assignments last"; ThirdM2 = 2; } } /* Making this variables and functions global */ let excitedE; let excitedM; let excitedH; function yesE() { excitedE == 3; } function mehE() { excitedE == 2; } function noE() { excitedE == 1; } function yesM() { excitedM == 3; } function mehM() { excitedM == 2; } function noM() { excitedM == 1; } function yesH() { excitedH == 3; } function mehH() { excitedH == 2; } function noH() { excitedH == 1; } function timeH() { if (hard >= 1) { FirstH = "do your # hard assignments first"; FirstH2 = 1; } else { return; } /* Calling the functions here */ yesE(); mehE(); noE(); yesM(); mehM(); noM(); yesH(); mehH(); noH(); } function ExcitedE() { if (excitedE == 3 && FirstE2 == 3) { FirstE = "do your # easy assignments last"; } else if (excitedE == 3 && FirstE2 == 3 && easy >= 4) { ThirdE = "do your # easy assignments third"; } //easy: 1st of list & yes excited else if (excitedE == 2 && FirstE2 == 3) { SecondE = "do your # easy assignments Second"; } //easy: 1st of list & maybe excited else if (excitedE == 1 && FirstE2 == 3) { FirstE = "do your # easy assignments first"; } //easy: 1st of list & not excited else if (excitedE == 3 && SecondE2 == 3) { SecondE = "do your # easy assignments second"; } //easy: 2nd of list & yes excited else if (excitedE == 2 && SecondE2 == 3) { SecondE == "do your # easy assignments second"; } //easy: 2nd of list and maybe excited else if (excitedE == 1 && SecondE2 == 3) { FirstE == "do your # easy assignments first"; } //easy: 2nd and no if (excitedE == 3 && ThirdE2 == 3) { ThirdE == "do your # easy assignments third"; } //easy: 3rd of list & yes excited else if (excitedE == 3 && ThirdE2 == 3) { SecondE == "do your # easy assignments second"; } //easy: 3nd of list and maybe excited else if (excitedE == 2 && ThirdE2 == 3) { FirstE == "do your # easy assignments first"; } //easy: 3rd and no else if (excitedE == 1 && ThirdE2 == 3) { FirstE == "do your # easy assignments first"; } } function ExcitedM() { if (excitedM == 3 && FirstM2 == 3 && medium >= 2) { ThirdM == "do your # easy assignments third"; } else if (excitedM == 3 && FirstE2 == 3) { FirstM == "do your # easy assignments last"; } //medium: 1st of list & yes excited if (excitedM == 3 && SecondM2 == 3) { SecondM == "do your # easy assignments second"; } //medium: 2nd of list & yes excited else if (excitedM == 2 && SecondM2 == 3) { SecondM == "do your # easy assignments second"; } //medium: 2nd of list and maybe excited else if (excitedM == 1 && SecondM2 == 3) { FirstM == "do your # easy assignments first"; } //medium: 2nd and no if (excitedM == 3 && ThirdM2 == 3) { ThirdM == "do your # easy assignments third"; } //medium: 3rd of list & yes excited else if (excitedM == 2 && ThirdM2 == 3) { SecondM == "do your # easy assignments second"; } //medium: 3nd of list and maybe excited else if (excitedM == 3 && ThirdM2 == 3) { FirstM == "do your # easy assignments first"; } //medium: 3rd and no else { return; } } function ExcitedH() { if (excitedE == 3 && FirstH2 == 3 && hard === 1) { ThirdH == "do your # hard assignments last"; } else if (excitedE == 3 && FirstH2 == 3) { FirstH == "do your # hard assignments first"; } //hard: 1st of list & yes excited if (excitedE == 3 && SecondH2 == 3) { SecondH == "do your # hard assignments second"; } //hard: 2nd of list & yes excited else if (excitedE == 2 && SecondH2 == 3) { SecondH == "do your # hard assignments second"; } //hard: 2nd of list and maybe excited else if (excitedE == 1 && SecondH2 == 3) { FirstH == "do your # hard assignments first"; } //hard: 2nd and no if (excitedE == 3 && ThirdH2 == 3) { ThirdH == "do your # hard assignments third"; } //hard: 3rd of list & yes excited else if (excitedE == 2 && ThirdH2 == 3) { SecondH == "do your # hard assignments second"; } //hard: 3nd of list and maybe excited else if (excitedE == 3 && ThirdH2 == 3) { FirstH == "do your # hard assignments first"; } else { return; } //hard: 3rd and no } function showSchedule() { document.getElementById("HD1").style.display = "none"; document.getElementById("homeworkSchedule").style.display = "block"; } function showEnterBtn() { document.getElementById("save").style.display = "none"; document.getElementById("enter").style.display = "block"; } function showHard() { document.getElementById("MD1").style.display = "none"; document.getElementById("HD1").style.display = "block"; } function showMedium() { document.getElementById("ED1").style.display = "none"; document.getElementById("MD1").style.display = "block"; } /* makes schedule */ function makeHomeworkSchedule() { document.getElementById("first").innerHTML = FirstE + " " + FirstM + " " + FirstH; document.getElementById("second").innerHTML = SecondE + " " + SecondM + " " + SecondH; document.getElementById("third").innerHTML = ThirdE + " " + ThirdM + " "; ThirdH; } </script> <body> <div id="ED1"> <p> <center> Question 1, enter number? </center> </p> <center><input min="0" id="easy" type="number" /></center> <center> <p>This an other question, click one of the buttons?</p> <button class="button button3" onclick="yesE()">Yes</button> <button class="button button3" onclick="mehE()">Meh</button> <button class="button button3" onclick="noE()">No</button> </center> <h1></h1> <center> <button class="button button3" onclick="getValueOfEasy(); timeE(); ExcitedE(); showMedium(); " id="easyEnter" > Enter </button> </center> </div> <div id="MD1" style="display: none;"> <h1> Question 2? </h1> <input min="0" id="medium" type="text" /> <p>this is another question?</p> <button class="button button3" onclick="yesM()">Yes</button> <button class="button button3" onclick="mehM()">Meh</button> <button class="button button3" onclick="noM()">No</button> <h1></h1> <button class="button button3" onclick="getValueOfMedium(); showHard(); timeM(); ExcitedM();" id="mediumEnter" > Enter </button> </div> <div id="HD1" style="display: none;"> <p> Question 3? </p> <input min="0" id="hard" type="text" /> <p>WOah another question?;</p> <button class="button button3" onclick="yesH()">Yes</button> <button class="button button3" onclick="mehH()">Meh</button> <button class="button button3" onclick="noH()">No</button> <h1></h1> <button id="save" class="button button3" onclick="getValueOfHard(); timeH(); ExcitedH(); makeHomeworkSchedule():" > Save </button> </div> <div id="homeworkSchedule" style="display; none."> <p>... paragraphs..:</p> <h1>1st:</h1> <h5 id="first"></h5> <h1>2nd:</h1> <h5 id="second"></h5> <h1>3rd:</h1> <h5 id="third"></h5> </div> </body> </html>
// declare your variable in global
var firstVariable;
function A(firstVariable){
// do something
}
// call function
A(firstVariable)
// if u want to check is some variable equals some value use `===` instead of '='
// don't use `<!--- -->` in JS, use `//` or `/* -your comment- */`
您有幾個語法錯誤需要首先修復。 您在某些函數中缺少一些括號,您正在使用賦值運算符=
作為比較器,並且缺少timeE
的右括號。 在腳本可以正確運行之前,您需要修復錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.