JavaScript-声明全局变量
我声明变量:JavaScript-声明全局变量,javascript,variables,Javascript,Variables,我声明变量: var Cash_player=0 它用于以下功能: function send_stats(message) { document.querySelector("#stats").innerText = message; } send_stats(`Stats: Gold:${Cash_player} `) function win() { Cash_player += 15; send_stats() } 然后在其他功能中增加: function send_stat
var Cash_player=0
它用于以下功能:
function send_stats(message) {
document.querySelector("#stats").innerText = message;
}
send_stats(`Stats:
Gold:${Cash_player}
`)
function win() {
Cash_player += 15;
send_stats()
}
然后在其他功能中增加:
function send_stats(message) {
document.querySelector("#stats").innerText = message;
}
send_stats(`Stats:
Gold:${Cash_player}
`)
function win() {
Cash_player += 15;
send_stats()
}
但是,结果是
未定义
。您能帮助我增加值,然后使用send_stats()
显示它吗?您似乎想将innerText
更新为Cash\u player
,而无需反复将其传递给函数。将文本更新为Stats:Gold:${Cash\u player}
insidesend\u Stats
让Cash\u player=0;
函数send_stats(消息){
document.querySelector(“#stats”).innerText=`stats:Gold:${Cash#u player}`;
}
函数win(){
现金+玩家+=15;
发送_stats()
}
发送统计信息()
Win
如果要传递参数,则需要传递参数。您正在调用send_stats,没有参数:send_stats()
。因此,消息
是未定义的
。