javascript 从一个函数中获取变量以在另一个函数中使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10462962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:57:56  来源:igfitidea点击:

taking variables from one function to use in another function

javascriptfunctionvariables

提问by Paddy Hallihan

i'm creating a game in javascript for college and i have a button to call a function but i need it to include variables from another function

我正在用 javascript 为大学创建一个游戏,我有一个按钮来调用一个函数,但我需要它包含来自另一个函数的变量

if i put the function inside the other function where the variables are the button doesn't call it

如果我将函数放在变量是按钮的另一个函数中,则不会调用它

is there a way of doing this or to get the variables that were created in the first function and use them in the second function

有没有办法做到这一点或获取在第一个函数中创建的变量并在第二个函数中使用它们

it is a simple maths game where you get a random number a random sum and another random number (i.e. 5+7, 4*3, 2/6, etc,)

这是一个简单的数学游戏,你得到一个随机数、一个随机数和另一个随机数(即 5+7、4*3、2/6 等)

so i've got a function to show the random number using

所以我有一个函数来显示随机数

var ranNum1a=Math.floor(Math.random()*10);

i need the other function to check the users answer but i need to know what the random number was from the other function

我需要另一个函数来检查用户的答案,但我需要知道来自另一个函数的随机数是什么

回答by Starx

For simplest case, passing variables to another function might be the best approach to this problem.

对于最简单的情况,将变量传递给另一个函数可能是解决此问题的最佳方法。

Here is an illustration:

这是一个插图:

function f1(var1, var2) {
    //do you stuff then pass the variable to another function
    f2(var1, var2);
}

function f2(v1, v2) {
    // here v1 will get the value from var1 and v2 from var2 if passed from within the function f1
}

回答by gav_aus_web

I found this to be extremely helpful in relation to the original question:

我发现这对原始问题非常有帮助:

Return the value you wish to use in functionOne, then call functionOne within functionTwo and place the result into a fresh var. This should enable you to use the var declared in functionOne, within functionTwo.

返回您希望在 functionOne 中使用的值,然后在 functionTwo 中调用 functionOne 并将结果放入新的 var 中。这应该使您能够在 functionTwo 中使用在 functionOne 中声明的 var。

function functionOne() {
  var variableThree = 3;
  return variableThree;
}

function functionTwo() {
  var variableOne = 1;
  var var3 = functionOne();

  var result = var3 - variableOne;

  console.log(variableOne);
  console.log(var3);
  console.log('functional result: ' + result);
}

functionTwo();

回答by Rycore

You would need to make those variables global example:

您需要将这些变量设为全局示例:

var myValue;
function setValue()
{
    myValue = "test";
}

function getValue()
{
    alert(window.myValue); // yup, it's "test"
}

source..

来源..

回答by Pavel Strakhov

var ranNum1a;
function show() {
  ranNum1a = Math.floor(Math.random()*10);
  // show it
}

function check() {
  if (ranNum1a == 42) {
    alert("Ok");
  }
}