简体   繁体   English

在函数之间共享Javascript变量

[英]Sharing Javascript variables between function

So I have a few variables that I need to use within two separate functions. 所以我需要在两个独立的函数中使用一些变量。 The first function essentially uses the variables to calculate and then display something (a conjugated verb). 第一个函数主要使用变量来计算然后显示某些东西(一个共轭动词)。 The second function uses the variables to see if the user's answers were correct, and changes some HTML styling accordingly. 第二个函数使用变量来查看用户的答案是否正确,并相应地更改一些HTML样式。

However, these variables are calculated randomly, like this: 但是,这些变量是随机计算的,如下所示:

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

var tense = randomIntFromInterval(1, 6);
var person = randomIntFromInterval(1, 3);
var number = randomIntFromInterval(1, 2);
var voice = randomIntFromInterval(1, 2);

I can't declare them as global variables outside a function, because they need to be recalculated every time the first function is called. 我不能将它们声明为函数外的全局变量,因为每次调用第一个函数时都需要重新计算它们。 I can't declare them in their own function and call it inside the original two functions, because both of the functions needs the same values. 我无法在自己的函数中声明它们并在原始的两个函数中调用它们,因为这两个函数都需要相同的值。

How can I do this? 我怎样才能做到这一点?

I think you're asking how to pass a structure of values between two functions. 我想你问的是如何在两个函数之间传递一个值结构。 What you (probably) don't want to do is pass each one individually, although that is an option if you only have a few and they come from different sources. 你(可能)不想做的是单独传递每个,但如果你只有少数几个来自不同的来源,这是一个选项。 If they come from the same, or similar, sources then you can use an object with named keys like: 如果它们来自相同或类似的源,那么您可以使用具有命名键的对象,如:

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

function getRandomSections() {
  return {
    tense: randomIntFromInterval(1, 6),
    person: randomIntFromInterval(1, 3),
    number: randomIntFromInterval(1, 2),
    voice: randomIntFromInterval(1, 2)
  };
}

function doSomethingWithSections(sections) {
  // do some things
}

function doSomethingElseWithSections(sections) {
  // do some things
}

// Put it all together
var sections = getRandomSections();
doSomethingWithSections(sections);
doSomethingElseWithSections(sections); // using the same values in a second function

If they came from unrelated places, I would suggest using individual params (as suggested by @jfriend00 in the comments) and doing: 如果他们来自不相关的地方,我建议使用个人参数(如评论中@jfriend00所建议)并且做:

function doSomethingWith(tense, person, number, voice) {
  // do some things
}

doSomethingWithSections(getTense(), getPerson(), getNumber(), getVoice());

If you want to make the list of fields somewhat dynamic, you can make getRandomSections data-driven like: 如果你想使字段列表有点动态,你可以使getRandomSections数据驱动,如:

function getSections(names, cb) {
  var obj = {};
  names.forEach(function (name) {
    obj[name] = cb(x, y);
  });
  return obj;
}

// Used as
getSections(['tense', 'person', 'number', 'voice'], randomIntFromInterval);

Cant you do something like this- 不能这样做 -

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

function f1(t, p, n, v){ //arguments for function
    //something
}
function f2(t, p, n, v){
    //something
}

function main(){
    var tense = randomIntFromInterval(1, 6);
    var person = randomIntFromInterval(1, 3);
    var number = randomIntFromInterval(1, 2);
    var voice = randomIntFromInterval(1, 2);

    f1(tense, person, number, voice);
    f2(tense, person, number, voice);
}

main();

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

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