【发布时间】:2016-01-16 10:18:43
【问题描述】:
我想使用 javascript 创建记分牌。我创建了一个全局变量NaN。我还创建了一个带有if 语句的函数。如果变量是NaN,那么变量需要为0。然后变量将得到+1。当我单击按钮运行该函数时,结果为 1。但是当我重新单击该按钮并重新运行该函数时,它保持为 1 并且不添加 +1。谁能告诉我我做错了什么?
var score = score; //creates a global variable
function addscore(score) {
alert(score); // just for me to see what value the variable has
if ((score) = isNaN) {
var score = 0; // the variable will be a number
}
score++;
alert(score);
}
【问题讨论】:
-
var score = score; //creates a global variable--- o_O -
var score = score情节扭曲:这不会创建全局变量。
标签: javascript function variables global-variables var