简体   繁体   English

Javascript变量未在内部函数中定义

[英]Javascript variable not define inside function

I am making a javascript based life game, and I am trying to make a balance system for it. 我正在制作一个基于JavaScript的生活游戏,并且我正在尝试为此建立一个平衡系统。 When I click on my button that calls my function balance(), an error occurs where the console fails to find my defined variable. 当我单击调用我的函数balance()的按钮时,控制台将找不到我定义的变量,则会发生错误。 Please help here is my code: 请帮助,这是我的代码:

 var balance; function balance() { let newbalance = balance + 100; let balance = newbalance; document.getElementById("balance").innerHTML = (balance); } 
 <link href="https://fonts.googleapis.com/css?family=Mukta+Malar" rel="stylesheet"> <center> <h1 class="title">Life Game</h1> </center> <table> <tr> <td class="mainTd"> <h1 id="balance">0</h1> </td> <td class="mainTd"> <h1 id="state">Begger</h1> </td> </tr> </table> <button onclick="balance()">Click Me</button> 

you have named your function balance which is shadowing the variable outside. 您已将函数balance命名为外部变量。

after changing the name of function give some default value to balance , var balance = 0; 更改函数名称后,将一些默认值设置为balance, var balance = 0;

there are three issues. 有三个问题。

not initializing the balance variable 没有初始化余额变量

if you had not other issues, the code would still not work. 如果没有其他问题,该代码将仍然无法正常工作。 you create a variable balance without setting the variable thus it will be undefined . 您无需设置变量就可以创建变量balance ,因此它将是undefined Thus adding 100 to it will result in a NaN 因此,将其加100将NaN

> var balance
undefined
> balance + 100
NaN

re-assigning the balance variable 重新分配余额变量

At first you define a global uninitialised variable balance for storing the balance value. 首先,定义一个全局未初始化变量balance以存储余额值。 Then you define function balance which will be assigned to the same variable. 然后定义功能balance ,该功能balance将分配给同一变量。 Thus the variable balance will point to a function from now on. 因此,变量balance将从现在开始指向一个函数。

> var balance;
undefined
> balance
undefined
> function balance() {}
undefined
> balance
[Function: balance]

shadowing the balance variable 遮蔽余额变量

Inside a the function you define a block scoped( let ) variable balance . 在函数内部,您定义了一个块范围为( let )的变量balance This will be a completely new variable that exists only inside of the function, thus anything you do to it would not affect the balance variable outside of the function. 这将是一个仅存在于函数内部的全新变量,因此您对其执行的任何操作均不会影响函数外部的balance变量。 This is called shadowing and linters should warn about this. 这被称为遮蔽,棉短绒应该对此发出警告。

> var balance = 11;
undefined
> function calc() {
... let balance = 22;
... return balance;
... }
undefined
> balance
11
> calc()
22
> balance 
11 <- original balance is still the same

fixed code 固定码

 var balance = 0; // set initial value function calculateBalance() { // use unique name for function let newbalance = balance + 100; balance = newbalance; // use the global variable instead of defining a new one document.getElementById("balance").innerHTML = (balance); } 
 <link href="https://fonts.googleapis.com/css?family=Mukta+Malar" rel="stylesheet"> <center> <h1 class="title">Life Game</h1> </center> <table> <tr> <td class="mainTd"> <h1 id="balance">0</h1> </td> <td class="mainTd"> <h1 id="state">Begger</h1> </td> </tr> </table> <button onclick="calculateBalance()">Click Me</button> 

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

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