简体   繁体   English

为什么我的函数(调用时)总是以未定义的形式返回?

[英]Why is my function (when called) always returned as undefined?

I have written a function for a web-based game I am designing, but whenever I call this function, it returns as undefined.我为我正在设计的基于 Web 的游戏编写了一个函数,但是每当我调用这个函数时,它都会返回为 undefined。

 function getRnd(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function getOttomanRival() { return this.getRnd(1, 3) if (ottoman.rival == 1) { var ottoman.rival = "Mamluks" } if (ottoman.rival == 2) { var ottoman.rival = "Delhi" } if (ottoman.rival == 3) { var ottoman.rival = "Timurids" } function myFunction() { var ottoman = { rival: getOttomanRival() } document.GetElementById("ottomanRival").innerHTML = ottoman.rival() } }
 <button onclick="myFunction()">Click me</button> <p id="ottomanRival"></p>

//This function (get Rand) looks good
function getRnd(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getOttomanRival() {
  return this.getRnd(1, 3) // if you return from here, none of the 
                           // following sentences will be executed
                           // Also you expect the rival name to be returned
                           // So store the random number in a variable
                           // and use it in your if condition and return
                           // the rival name once its set.
                           // Also "this" is not required here

  if (ottoman.rival == 1) { // check the random number generated,
                            // not the rival name against 1
    var ottoman.rival = "Mamluks"; // there is no syntax that declares
                                   // a variable "ottoman.rival" "." is 
                                   // not a valid variable name
                                   // use a simple variable
                                   // This function doesn't even need to know about
                                   // the structure of final object
  }
  if (ottoman.rival == 2) {
    var ottoman.rival = "Delhi"
  }
  if (ottoman.rival == 3) {
    var ottoman.rival = "Timurids"
  } 

  // missing a closing } for getOttomanRival

  function myFunction() {
    var ottoman = {
      rival: getOttomanRival()
    }
    document.GetElementById("ottomanRival").innerHTML = ottoman.rival() 
       // you probably meant "ottoman.rival"
       // without "()" as rival holds the result of `getOttomanRival()"
       // which is a string and not a function
  }
}

 function getRnd(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function getOttomanRival() { var x = getRnd(1, 3), rival; if (x == 1) { rival = "Mamluks" } if (x == 2) { rival = "Delhi" } if (x == 3) { rival = "Timurids" } return rival; } function myFunction() { var ottoman = { rival: getOttomanRival() }; document.getElementById("ottomanRival").innerHTML = ottoman.rival; }
 <button onclick="myFunction()">Click me</button> <p id="ottomanRival"></p>

The reason myFunction is undefined is because it is wrapped inside another function getOttomanRival() myFunction未定义的原因是因为它包含在另一个函数getOttomanRival()

as @hriziya said, you could separate the functions like this正如@hriziya 所说,你可以像这样分离功能

function getRnd(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getOttomanRival() {
  return this.getRnd(1, 3)
  if (ottoman.rival == 1) {
    var ottoman.rival = "Mamluks"
  }
  if (ottoman.rival == 2) {
    var ottoman.rival = "Delhi"
  }
  if (ottoman.rival == 3) {
    var ottoman.rival = "Timurids"
  }
}

function myFunction() {
  var ottoman = {
    rival: getOttomanRival()
  }
  document.GetElementById("ottomanRival").innerHTML = ottoman.rival()
}

Or if you still need to keep the function nested, you could call it in your HTML like this:或者,如果您仍然需要保持函数嵌套,您可以像这样在 HTML 中调用它:

<button onclick="(new getOttomanRival()).myFunction()">Click me</button>

Provided myFunction() is updated like this提供myFunction()像这样更新

this.myFunction = function() {
  var ottoman = {
    rival: getOttomanRival()
  }
  document.GetElementById("ottomanRival").innerHTML = ottoman.rival()
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM