函数不起作用,如果我在函数外调用 console.log(window.Test),我会得到 undefined..
当我在下面这段代码中调用函数内部时工作--
var num = 0;
function clientseed() {
var a = "test";
num++;
window.Test = a + "-" + num;
console.log(window.Test)
}
但不是下面的那个——即使我使用的是 window.这是用于定义全局变量但我得到未定义的结果..如果我点击按钮 3 次并转到控制台并输入 console.log(window.Test) 它也显示未定义
var num = 0;
function clientseed() {
var a = "test";
num++;
window.Test = a + "-" + num;
}
console.log(window.Test)
HTML
<button onclick="clientseed()">Test</button>
最佳答案
even though im using window. which is for defining global variable but I get undefined result..
您正在使用 window
,这很好,但是,甚至在您声明变量之前,您的日志语句就已执行。
案例 1(顶部代码片段):
Button click {
// declared the variable
// printed.
}
案例 2:
Button click {
// declared the variable
}
// printed. -- well the button not yet clicked. I don't know what is the variable you are asking me to print.
关于javascript - 如果在外部调用,函数将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42818268/