【发布时间】:2022-01-20 12:07:48
【问题描述】:
正如标题所说,如果它是全局的,我不能在我的函数上使用变量“countDash”,只能在本地使用。应该是这样吗?
我错过了什么?提前致谢。
//count
let countEl = document.getElementById("count-el");
let saveEl = document.getElementById("save-el");
let count = 0;
//message to user
let username = "Mr. Unknown";
let message = "You have three new notifications";
let messageToUser = `${message}, ${username}!`;
//welcome message
let welcomeEl = document.getElementById("welcome-el");
let name = "Eduardo";
let greeting = "Welcome back";
welcomeEl.innerHTML = `${greeting}, ${name}!`;
function increment() {
count += 1;
countEl.innerHTML = count;
}
// let countDash = ` ${count} -`; //does not work
function save() {
let countDash = ` ${count} -`; //it only works if I have it here localy
saveEl.innerHTML += countDash;
}
【问题讨论】:
-
“不起作用”究竟是什么意思?有什么事吗?是否报告错误?
-
您应该添加更多详细信息
标签: javascript function variables global-variables local-variables