如何使用三元运算符退出函数

问题描述 投票:1回答:1

我这里有未完成的代码

document.querySelector('.btn-hold').addEventListener('click', function() {
    scores[activePlayer] += roundScore
    document.querySelector("#score-" + activePlayer).textContent = scores[activePlayer]
    //checking if the player won the game
    scores[activePlayer] === 20? /*exit the function*/ :  /*move to the next player*/ nextPlayer()
})

我尝试过'return',但它有一个错误。

javascript function dom exit conditional-operator
1个回答
1
投票

三元运算符对于控制流来说是不必要的,如果您尝试从运算符返回值,则它会更加有用。您应该考虑将其简化为简单的if语句。

if (scores[activePlayer] !== 20)
    nextPlayer();

并且如果为假,则让函数自然退出。

© www.soinside.com 2019 - 2024. All rights reserved.