更有效地添加JavaScript变量?

Jag*_*tar -2 javascript math

如果我有两个和变量的总和变量.是否有更简单的方法来获得总和?

例:

var first_one = 5;
var first_two = 5;

var second_one = 5;
var second_two = 5;

var firstTotal = first_one + first_two;
var secondTotal = second_one + second_two;

var total = firstTotal + secondTotal;
return total;
Run Code Online (Sandbox Code Playgroud)

tym*_*eJV 10

创建一个名为的函数 sum()

function sum(val1, val2) {
    return val1 + val2;
}
Run Code Online (Sandbox Code Playgroud)

叫它

var firstTotal = sum(first_one, first_two);
var secondTotal = sum(second_one, second_two);

var total = sum(firstTotal, secondTotal);
Run Code Online (Sandbox Code Playgroud)