提示返回为未定义的函数。 (范围问题) [英] Prompt return as undefined in a function. (scope issue)

查看:105
本文介绍了提示返回为未定义的函数。 (范围问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个范围问题,因为我试图在 firstPartCook 内设置我的函数 userPrmpt ,它可以工作。我把它放在外面,而不是。所以它正在阅读,但没有保留返回的内容。我认为通过将它放在测试变量中它可以工作,但它不会。



这是我的代码



HTML $ b

 <!DOCTYPE html> 
< html>
< head>
< title> Race Makin'< / title>
<! - 链接到此脚本的JS部分 - >
< script src =riceMakin.js>< / script>
< / head>
< body>
<! - 不适合任何幻想。只专注于力学 - >
< h1>让我煮一些该死的米饭< / h1>
< h2>要开始煮饭,请点击开始< / h2>
< button onclick =firstPartCook()>开始< /按钮>
< h3>以下说明制米机的状态:< / h3>
<! - 与JS我有什么介于两者之间,从开启和关闭切换 - >

< / body>
< / html>

JS
< pre class =lang-js prettyprint-override> // Global Vars here
//提示用户继续操作脚本。要使用返回的内容,请设置为var
函数userPrmpt(userResponse){
prompt(userResponse);
}

//此功能用于向DOM添加类型。
函数insertCopy(copy){
var paragraphCreate = document.createElement(p);
var copyNode = document.createTextNode(copy);
paragraphCreate.appendChild(copyNode);
var idSelecter = document.getElementById(print);
// print是跨度的id标记
idSelecter.appendChild(paragraphCreate);
}

//这是我们开始处理脚本的机制的地方
函数firstPartCook(){
// var userAnswer = prompt(Welcome to稻米制造商,想开始做饭?);
var test = userPrmpt(Hello);
if(test ==yes){
console.log(test);
insertCopy(It worked);
} else {
console.log(test);
insertCopy(Nope);



$ div $解析方案

你必须返回提示中的值,否则函数将返回 undefined ,这是任何函数的默认返回值,没有其他值返回

  function userPrmpt(userResponse){
return prompt(userResponse);
}


I believe it's a scope issue because I tried setting my function userPrmpt inside firstPartCook and it works. I set it outside, and it doesn't. So it's reading, but not keeping what is returned. I thought by placing it in the test var that it would work, but it doesn't.

Here's my code

HTML

<!DOCTYPE html>
<html>
 <head>
  <title> Race Makin' </title>
  <!-- Link to the JS portion for this script -->
  <script src="riceMakin.js"></script>
 </head>
 <body>
  <!-- not going for anything fancy. Just focusing on mechanics -->
  <h1>Lets cook some damn rice</h1>
  <h2> To start cooking Rice, please hit start</h2>
  <button onclick="firstPartCook()">Start</button>
  <h3>Below explains the status on the Rice Making Machine:</h3>
  <!-- with JS i have what is inbetween the spans, switching from on and off -->
  <p id="print">Status: Turned <span id="print">Off</span> </p>     
 </body>
</html>

JS

//Global Vars here
//Promp for the User to continue throught the script. To use what is returned, set to a var
function userPrmpt(userResponse) {
   prompt(userResponse);
}

// This function is for adding type to the DOM.
function insertCopy (copy) {
   var paragraphCreate = document.createElement("p");
   var copyNode = document.createTextNode(copy);
   paragraphCreate.appendChild(copyNode);
   var idSelecter = document.getElementById("print");
   //print is the id tag of the span 
   idSelecter.appendChild(paragraphCreate);
}

//This is where we start working on the mechanics of the script    
function firstPartCook() {
   //var userAnswer = prompt("Welcome to the Rice Maker, want to start making rice?");
   var test = userPrmpt("Hello");
   if (test == "yes") {
      console.log(test);
      insertCopy("It worked");
   } else {
      console.log(test);
      insertCopy("Nope");
   }
}

解决方案

You have to return the value from the prompt, otherwise the function will just return undefined, which is the default return value of any function that has no other value returned from it

function userPrmpt(userResponse){
    return prompt(userResponse);
}

这篇关于提示返回为未定义的函数。 (范围问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆