用户正在将值传递给函数,我正在尝试将该值分配给全局变量,但无法弄清楚如何执行此操作。
var a;
function changeValue(newValue) {
a = newValue;
}
alert("Value of 'a' outside the function " + a); //I want this to output the value of newValue
答案 0 :(得分:1)
您的功能正常,您只需要调用它:
var a;
function changeValue(newValue) {
a = newValue;
}
changeValue(100); // Function call changes the value of a to 100
alert("Value of 'a' outside the function " + a); // 100
答案 1 :(得分:0)
你在这里:
var a;
b = 0;
function changeValue(newValue) {
a = newValue;
b = newValue;
}
changeValue('a');
changeValue('b');
alert("Value of 'a' outside the function " + a);
alert("Value of 'b' outside the function " + b);
希望得到这个帮助。