如何多次分配相同的值
How to assign same value in multiple times
当我需要为目标变量分配一些长值时,我的脚本中有很多次。例如:
var longVar;
// Now some switch´s case
case:
longVar = "Some value, which is very, very long";
break;
// Another case in another switch, so cannot be used writing cases side by side
case:
longVar = "Some value, which is very, very long";
break;
但是不太实用。我目前正在使用函数设置值:
case:
setVariable();
break;
function setVariable() {
longVar = "Some value, which is very, very long";
}
这(在我看来)比每次都写值要好。是好还是更好,比如这个?:
var valueOfLongVar1 = "Some value, which is very, very long";
// Then in cases
case:
longVar = valueOfLongVar1;
break;
请问,更好的是 function() 解决方案,第二个变量解决方案,还是另一个?感谢回复
如果多次给变量赋值,最好给变量赋值槽常量。
你不应该像你描述的那样使用方法。
已更新:代码片段
const staticText = 'hello world'
let variableText = staticText
当我需要为目标变量分配一些长值时,我的脚本中有很多次。例如:
var longVar;
// Now some switch´s case
case:
longVar = "Some value, which is very, very long";
break;
// Another case in another switch, so cannot be used writing cases side by side
case:
longVar = "Some value, which is very, very long";
break;
但是不太实用。我目前正在使用函数设置值:
case:
setVariable();
break;
function setVariable() {
longVar = "Some value, which is very, very long";
}
这(在我看来)比每次都写值要好。是好还是更好,比如这个?:
var valueOfLongVar1 = "Some value, which is very, very long";
// Then in cases
case:
longVar = valueOfLongVar1;
break;
请问,更好的是 function() 解决方案,第二个变量解决方案,还是另一个?感谢回复
如果多次给变量赋值,最好给变量赋值槽常量。
你不应该像你描述的那样使用方法。
已更新:代码片段
const staticText = 'hello world'
let variableText = staticText