Javascript 从一个函数中获取值并将其放入另一个函数中

Javascript 从一个函数中获取值并将其放入另一个函数中,javascript,html,global-variables,Javascript,Html,Global Variables,我需要将extractVariable函数中当前保存的inputScore值放入assignValue函数。此函数将inputScore值提供给Index.html页面。我试图将inputScore存储在全局对象中以解决范围问题,但在assignValue中,global.scoreValue变量未定义。请问如何将inputScore转换成赋值?编程新手-欢迎任何帮助。谢谢 global = {}; function extractVariable(inputScore) { glo

我需要将extractVariable函数中当前保存的inputScore值放入assignValue函数。此函数将inputScore值提供给Index.html页面。我试图将inputScore存储在全局对象中以解决范围问题,但在assignValue中,global.scoreValue变量未定义。请问如何将inputScore转换成赋值?编程新手-欢迎任何帮助。谢谢

global = {};


function extractVariable(inputScore) {

   global['scoreValue'] = inputScore;

};


function assignValue() { 

    document.getElementById("inputScore").value = global.scoreValue;

};
谢谢大家的帮助。我很快就能解决我需要做的事情了。问题似乎是将inputScore放入Index.html页面。我应该在第一时间发布我所有的代码。道歉。index.html是一个单独的文件,它有一个指向javascript文件Game.js的链接。我已经测试了链接,它正在工作。当在Game.js中按下按钮时,index.html将加载、读取Game.js中的assignValue函数,并将玩家分数inputScore插入表单中的输入值中。目前,表格中插入的内容包括: 我不明白为什么它不起作用。我已经包含了下面两个文件中的代码。再次感谢您的帮助

Game.js代码:

function extractVariable(inputScore) {

return inputScore;


};


function assignValue(inputScore) {  
     document.getElementById("playerScore").value = inputScore;

};



var CrystalRunner = CrystalRunner || {};

CrystalRunner.GameState = {

init: function() {
  //...code here
  }, 


create: function() {
 //...code here
  },  


 update: function() {  
//..code here
//check if the player needs to die
      if(this.player.top >= this.game.world.height) {
         this.gameOver();
      }
  },  


 gameOver: function(){

    //..code here

    this.updateHighscore();

    //..code here

   },


  updateHighscore: function(){
    this.highScore = +localStorage.getItem('highScore');


    if(this.highScore < this.myScore){
            this.highScore = this.myScore;

            this.inputScore = this.highScore; 


            this.submitScoreButton = this.game.add.sprite(this.game.world.centerX-135, this.game.world.centerY+100, 'submitScoreButton');

            this.submitScoreButton.events.onInputUp.add(function() {

                    window.location.href = "index1.php"; 

              }, this);

        extractVariable(this.inputScore);
      }


      localStorage.setItem('highScore', this.highScore);
  },


};
在Game.js中,我发现如果我将一个值硬编码到extractVariable函数中,请参见下文,硬编码的值将传递到Index.html中标记的value属性中,这就是我希望它执行的操作。但是,我仍然无法理解为什么这只适用于硬编码的值

function extractVariable(inputScore) {
  inputScore = 27; //hard coded value works. Why?
return inputScore; 

};


function assignValue(inputScore) {  
  console.log(inputScore); //this console.logs the hard coded value from 
  //extractVariable like it should do
     document.getElementById("playerScore").value = inputScore;

};

您必须在assignValue中调用extractVariable

var全局={}; 函数extractVariableinputScore{ 全局['scoreValue']=输入分数; }; 函数赋值{ 变量“测试”; document.getElementByIdinputScore.value=global.scoreValue; }; 让渡价值;
您可以通过从extractVariable返回值,然后将其作为参数传递到assignValue中来实现这一点:


请使用es6和let的作用域-以下是一个很好的示例:

let scoreValue = 0;

function extractVariable(inputScore) {

    scoreValue = inputScore;

};

function assignValue() {

    console.log(scoreValue);

};

extractVariable(200);
assignValue();
我不明白为什么它不起作用

您有一个id为inputScore的元素

您正在指定应使用inputScore作为参数调用extractVariable:

<body onload="assignValue(extractVariable(inputScore))" class="bg"> 

我终于解决了。这是任何感兴趣的人的解决方案。我已经在Game.js代码中添加了注释来说明我所做的事情。Index.html保留在我在问题中编辑它时的状态。谢谢所有帮助过我的人

    function extractVariable(inputScore) {

  inputScore = +localStorage.getItem('highScore'); //we then define inputScore again 
by getting the score stored on local storage

  return inputScore; 

};



function assignValue(inputScore) {  

     document.getElementById("playerScore").value = inputScore;

};



var CrystalRunner = CrystalRunner || {};

CrystalRunner.GameState = {

init: function() {

  },


create: function() {

  },   


update: function() {  

      if(this.player.top >= this.game.world.height && this.counter === 0 || 
 this.player.left <= 0 && this.counter === 0) {
         this.gameOver();
      }


  },     


  gameOver: function(){

          this.updateHighscore();



      var style = {font: '40px Arial', fill: '#fff'};
     this.add.text(this.game.width/2, this.game.height/4, 'GAME OVER', 
style).anchor.setTo(0.5);


      style = {font: '30px Arial', fill: '#fff'};
      this.add.text(this.game.width/2, this.game.height/4 + 50, 'High score: ' + 
this.highScore, style).anchor.setTo(0.5);

      this.add.text(this.game.width/2, this.game.height/4 + 80, 'Your score: ' + 
this.myScore, style).anchor.setTo(0.5);

      style = {font: '18px Arial', fill: '#fff'};
      this.add.text(this.game.width/2, this.game.height/4 + 120, 'Tap/click to play 
again', style).anchor.setTo(0.5);


      this.game.input.onDown.addOnce(this.restart, this);


    }, this);

    gameOverPanel.start();
    this.counter++;

  },


  restart: function(){

    this.game.state.start('Game');
  },

  updateHighscore: function(){
    this.highScore = +localStorage.getItem('highScore');


    if(this.highScore < this.myScore){
            this.highScore = this.myScore;

      localStorage.setItem('highScore', this.highScore);

      this.inputScore= +localStorage.getItem('highScore'); //this input.Score needs 
      //to get it's value from localStorage and not this.highScore


            this.submitScoreButton = this.game.add.sprite(this.game.world.centerX- 
   135, this.game.world.centerY+100, 'submitScoreButton');
            this.submitScoreButton.inputEnabled = true;
            this.submitScoreButton.fixedToCamera = true;

            extractVariable(this.inputScore); //call extractVariable

            this.submitScoreButton.events.onInputDown.add(function() {


                        window.location.href = "index1.php";


                  }, this);

            }

       },


};

但在assignValue内部,global.scoreValue变量未定义,如果在extractVariable之前调用assignValue,则会发生这种情况。由于您没有提供一个服务,我们无法提供太多帮助。到目前为止,您发布的代码还可以。添加您的html也可以,似乎与您当前的代码一起工作。似乎是一个问题。这些函数是否如“this function为Index.html页面提供inputScore的值”所示位于不同的页面上?为什么不从extractVariable函数返回该值?然后可以在assignValue中调用它并使用返回值。或者,如果在外部的任何地方调用它,则使用它从那里返回值。如果可能的话,extractVariable应该只返回值,而不是使用全局变量。吹毛求疵:这不是函数组合。函数合成创建了一个新函数。啊,我可以称之为复合函数还是不…?老实说,idk。您只是将一个函数的返回值传递给下一个函数。不确定是否有特殊名称'\_ツ_/好的,谢谢你告诉我。我已经编辑了我的回答你好,塞巴斯蒂安,我感谢所有能得到的帮助。非常感谢。不知道是谁否决了你,当然不是我!没问题——因为几周的计划不是线性的——你可以通过一千种方式达到你的目标。我试着给出与你走的方式相匹配的答案-对我来说-你的案例看起来像一个表单应用程序,所以我会使用JS框架来处理所有的东西,编写类等等-但这对于你真正需要的案例来说可能太多了-所以我试着给你一些提示来解决你当前的架构。。。继续编码;顺便说一句,当您使用正确的作用域时,您可以以相同的方式调用函数中的函数:assignValueextractVariable200;作品谢谢你,塞巴斯蒂安。我通过添加更多的代码编辑了我的问题,并更清楚地解释了我要做的事情。我本来应该这么做的。尽管如此,感谢大家的帮助,我离解决这个问题更近了。非常感谢Felix的回复。我不知道HTML元素ID是全局变量。我已将输入id更改为playerScore。在index.html文件中调用onload的assignValue函数中的inputScore参数引用了Game.js文件中extractVariableinputScore中使用的参数。但我不知道是否需要在那里。html一直告诉我inputScore是未定义的。我只需要将返回到Game.js中extractVariableinputScore的值放入
index.html.Well中的输入标记,浏览器是正确的,当页面加载时,全局范围内没有变量inputScore。在页面加载时,您期望值是多少?或者换句话说:在页面加载时,您希望在输入字段中显示哪个值。如果输入字段最初应该是空的,只需删除调用。嗨,Felix,我已经对我的问题做了一些编辑,解释了我现在的位置。我已经按照您的建议从index.js中的调用中删除了inputscore。谢谢你在这方面的帮助。如果发现如果我在Game.js中将一个值硬编码到inputScore中,代码现在就可以工作了。如果我没有硬编码一个值,那么它就不会。知道为什么吗?Game.js中的extractVariable肯定会得到一个值,因为它会记录一个值。
let scoreValue = 0;

function extractVariable(inputScore) {

    scoreValue = inputScore;

};

function assignValue() {

    console.log(scoreValue);

};

extractVariable(200);
assignValue();
<body onload="assignValue(extractVariable(inputScore))" class="bg"> 
    function extractVariable(inputScore) {

  inputScore = +localStorage.getItem('highScore'); //we then define inputScore again 
by getting the score stored on local storage

  return inputScore; 

};



function assignValue(inputScore) {  

     document.getElementById("playerScore").value = inputScore;

};



var CrystalRunner = CrystalRunner || {};

CrystalRunner.GameState = {

init: function() {

  },


create: function() {

  },   


update: function() {  

      if(this.player.top >= this.game.world.height && this.counter === 0 || 
 this.player.left <= 0 && this.counter === 0) {
         this.gameOver();
      }


  },     


  gameOver: function(){

          this.updateHighscore();



      var style = {font: '40px Arial', fill: '#fff'};
     this.add.text(this.game.width/2, this.game.height/4, 'GAME OVER', 
style).anchor.setTo(0.5);


      style = {font: '30px Arial', fill: '#fff'};
      this.add.text(this.game.width/2, this.game.height/4 + 50, 'High score: ' + 
this.highScore, style).anchor.setTo(0.5);

      this.add.text(this.game.width/2, this.game.height/4 + 80, 'Your score: ' + 
this.myScore, style).anchor.setTo(0.5);

      style = {font: '18px Arial', fill: '#fff'};
      this.add.text(this.game.width/2, this.game.height/4 + 120, 'Tap/click to play 
again', style).anchor.setTo(0.5);


      this.game.input.onDown.addOnce(this.restart, this);


    }, this);

    gameOverPanel.start();
    this.counter++;

  },


  restart: function(){

    this.game.state.start('Game');
  },

  updateHighscore: function(){
    this.highScore = +localStorage.getItem('highScore');


    if(this.highScore < this.myScore){
            this.highScore = this.myScore;

      localStorage.setItem('highScore', this.highScore);

      this.inputScore= +localStorage.getItem('highScore'); //this input.Score needs 
      //to get it's value from localStorage and not this.highScore


            this.submitScoreButton = this.game.add.sprite(this.game.world.centerX- 
   135, this.game.world.centerY+100, 'submitScoreButton');
            this.submitScoreButton.inputEnabled = true;
            this.submitScoreButton.fixedToCamera = true;

            extractVariable(this.inputScore); //call extractVariable

            this.submitScoreButton.events.onInputDown.add(function() {


                        window.location.href = "index1.php";


                  }, this);

            }

       },


};