如何使用函数

时间:2018-12-13 05:48:50

标签: javascript functional-programming

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 

请帮助我

2 个答案:

答案 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);