function switchValue (a, b) {
return [b,a] = [a,b]
}
var a = 'computer'
var b = 'laptop'
switchValue(a, b)
console.log("a = " +a)
console.log("b = " +b)
如何更改此变量,即输出:
a = laptop
b = komputer
请帮助我
答案 0 :(得分:0)
尝试一下
function switchValue(a, b) {
let c = a;
let a = b;
let b = c;
return [a, b];
}
答案 1 :(得分:0)
您可以这样操作,但通常不建议使用全局变量
var a = 'computer'
var b = 'laptop'
function switchValue (val1, val2) {
let c = val1;
a = val2;
b = c;
}
switchValue(a, b)
console.log("a = " + a);
console.log("b = " + b);
没有全局变量
function switchValue (a, b) { return [b,a] }
var a = 'computer'
var b = 'laptop'
let [A,B] = [...switchValue(a, b)]
console.log("a = " + A);
console.log("b = " + B);