简体   繁体   English

为什么当我从两个函数之间移动一个变量并将其放在两个函数之上时,function 2 会变为未定义?

[英]Why when I move a variable from in between 2 functions and place it above both functions does function 2 become undefined?

I'm very new to JavaScript and I'm having a hard time understanding why my code is coming back as undefined.我是 JavaScript 的新手,我很难理解为什么我的代码返回时未定义。 I also can't seem to get the dropdown to function as it should.我似乎也无法按应有的方式将下拉列表显示为 function。

My CodePen: https://codepen.io/TiffSmith126/pen/KKZxdma我的 CodePen: https://codepen.io/TiffSmith126/pen/KKZxdma

Problem 1) When I move the variable for the dropdown to above window.firstFunction instead of placing it below window.firstFunction and above window.secondFunction, window.secondFunction becomes undefined.问题 1) 当我将下拉列表的变量移动到 window.firstFunction 以上而不是将其放置在 window.firstFunction 以下和 window.secondFunction 以上时,window.secondFunction 变为未定义。 I would like to know what's wrong and why this keeps happening in my code even though the function is defined.我想知道出了什么问题以及为什么即使定义了 function 也会在我的代码中发生这种情况。

Problem 2) This is my first time creating a dropdown and it's not working as desired.问题 2) 这是我第一次创建下拉菜单,它没有按预期工作。 I would like it to select an option and have it display one of two results of the first function. For example, in my code the two results are item1 and item2.我想给 select 一个选项,让它显示第一个 function 的两个结果之一。例如,在我的代码中,两个结果是 item1 和 item2。 I want my user to be able to select which item they want.我希望我的用户能够通过 select 找到他们想要的商品。

 const fruits = ["apple", "orange", "banana"]; const vegetables = ["carrot", "corn", "kale"]; const subjects = ["math", "science", "reading", "writing"]; let item1; let item2; var menu = document.getElementById("dropdown"); menu.addEventListener("change", firstFunction()); if (menu.value == "1") { void 0; } else if (menu.value == "2") { document.getElementById("instance").innerHTML = item1; } else if (menu.value == "3") { document.getElementById("instance").innerHTML = item2; } window.firstFunction = function () { var ranFruit = Math.floor(Math.random() * 3); var ranVegetable = Math.floor(Math.random() * 3); let bowl = []; let plate = []; var situation1 = "some text " + fruits[ranFruit] + "some more text " + vegetables[ranVegetable]; bowl.push(situation1); var situation2 = "some text " + fruits[ranFruit] + "some more text" + vegetables[ranVegetable]; plate.push(situation2); var situation3 = "some text " + fruits[ranFruit] + "some more text" + vegetables[ranVegetable]; bowl.push(situation3); var situation4 = "some text " + fruits[ranFruit] + "some more text" + vegetables[ranVegetable]; plate.push(situation4); var ranBowls = Math.floor(Math.random() * 2); var ranPlates = Math.floor(Math.random() * 2); var item1 = bowl[ranBowls]; var item2 = plate[ranPlates]; }; firstFunction(); window.secondFunction = function () { var ranSubject = Math.floor(Math.random() * 4); var sentence = "some more text" + subjects[ranSubject] + "even more great text"; document.getElementById("sentence").innerHTML = sentence; }; secondFunction();
 <head> <title>Generator</title> <head> <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet"> </head> <body> <div class="generator-container"> <div class="title">Generator </div> <div class="container-one"> <span>Details</span> <select id="dropdown"> <option value="1">Select</option> <option value="2">Bowls</option> <option value="3">Plates</option> </select> <button type="button" onclick="firstFunction()">Generate<i class=" fa fa-generate" aria-hidden="true"></i></button> <br> <span id='instance'></span> </div> <div class="container-two"><span>Additional Data</span> <button type="button" onclick="secondFunction()">Generate<i class=" fa fa-generate" aria-hidden="true"></i></button> <br> <span>Subject Sentence:</span> <br> <span id="sentence"></span><br><br> </div> </body>

Since JavaScript is an script language & is run in order it does not know about functions that it hasn't read yet.由于 JavaScript 是一种脚本语言,因此运行时它不知道尚未读取的函数。

Your calling firstFunction before it is defined when you add the listener to the menu.当您将侦听器添加到菜单时,您在定义 firstFunction 之前调用它。
Moving移动

menu.addEventListener("change", firstFunction());

to the end makes it work到最后使它起作用

const fruits = ["apple", "orange", "banana"];
const vegetables = ["carrot", "corn", "kale"];
const subjects = ["math", "science", "reading", "writing"];

let item1;
let item2;

var menu = document.getElementById("dropdown");
if (menu.value == "1") {
  void 0;
} else if (menu.value == "2") {
  document.getElementById("instance").innerHTML = item1;
} else if (menu.value == "3") {
  document.getElementById("instance").innerHTML = item2;
}


window.firstFunction = function () {
  var ranFruit = Math.floor(Math.random() * 3);
  var ranVegetable = Math.floor(Math.random() * 3);
  let bowl = [];
  let plate = [];

  var situation1 =
    "some text " +
    fruits[ranFruit] +
    "some more text " +
    vegetables[ranVegetable];
  bowl.push(situation1);

  var situation2 =
    "some text " +
    fruits[ranFruit] +
    "some more text" +
    vegetables[ranVegetable];
  plate.push(situation2);

  var situation3 =
    "some text " +
    fruits[ranFruit] +
    "some more text" +
    vegetables[ranVegetable];
  bowl.push(situation3);

  var situation4 =
    "some text " +
    fruits[ranFruit] +
    "some more text" +
    vegetables[ranVegetable];
  plate.push(situation4);

  var ranBowls = Math.floor(Math.random() * 2);
  var ranPlates = Math.floor(Math.random() * 2);

  var item1 = bowl[ranBowls];
  var item2 = plate[ranPlates];
};

firstFunction();


window.secondFunction = function () {
  var ranSubject = Math.floor(Math.random() * 4);
  var sentence =
    "some more text " + subjects[ranSubject] + " even more great text";
  document.getElementById("sentence").innerHTML = sentence;
};

secondFunction();
menu.addEventListener("change", firstFunction());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我一开始没有声明它为变量时,为什么在函数内部定义的x会成为全局变量? - Why does x, defined inside a function, become a global variable when I didn't declare it to be a variable in the first place? 为什么在我调用functions属性时未定义? - Why is this undefined when I call a functions property? 当我将函数抽象为命名函数时,为什么未定义此回调? - Why is this callback undefined when I abstract my functions to named functions? 为什么我的全局变量在函数之间不携带其值 - why does my global variable not carry its value between functions 为什么当下拉菜单中的值更改时状态变量变为未定义反应? - Why does the state variable become undefined when the value is changed in the dropdown menu react? 为什么我的post函数无法访问其上方的变量? - Why can't my post functions access a variable above it? 为什么我的函数返回“ undefined” - why does my functions returning “undefined” 为什么变量在$(document).keypress中变得未定义? - Why does variable become undefined inside of $(document).keypress? 为什么我的异步函数仅在更改被调用函数的语法后才能正常工作? - Why does my Async function only work correctly when I change my called functions syntax? 为什么我在 javascript 中记录 class 和 function 中的函数有不同的表达方式? - Why does functions in class and function have different expression when I log them in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM