出现js错误未捕获的TypeError:roundScore不是函数

Joh*_*ohn 0 javascript

只是想学习js并编写一些迷你游戏逻辑,但我被困住了,因为contantley遇到此错误 Uncaught TypeError: roundScore is not a function

不知道为什么这是因为我确实有问题的功能:

var playerScore = 0;
var player = 0;
var roundScore = 0;

function rollDice() {
  var dice;
  dice = Math.floor((Math.random() * 6) + 1);
  return dice;
}

function playerTurn() {
  player === 0 ? player = 1 : player = 0;
}

function roundScore() {
  rollDice() !== 1 ? roundScore += rollDice() : (playerTurn(), roundScore = 0);
  return roundScore;
}

document.getElementById('rollDice').addEventListener('click', function () {
    document.getElementById('round-score-' + player).innerHTML = roundScore();
});
Run Code Online (Sandbox Code Playgroud)

有人可以看到我为什么收到此错误的信息吗?谢谢

sha*_*ses 6

您有一个函数和一个名为的变量roundScore。由于JavaScript的编译方式,您的函数被提升到文件顶部,即roundScore0,而不是函数。因此,将函数或变量命名为其他名称,就可以了。