javascript中的常量变量

时间:2017-10-13 11:33:39

标签: javascript variables constants

我遇到了一个小问题,我有一个名为'小部件'的变量,我想将它的原始值存储在'storeWigets'中,即使我更新了'小部件'的值,我的变量'storeWidgets'仍保留在'小部件'的原始值。

var widgets=["1","2","3","4"];
var storeWidgets;
storeWidgets = widgets;
widgets[0]=0;
alert(storeWidgets);

我有一个example on jsfiddle,我需要最后我的'storeWidgets'保留值[“1”,“2”,“3”,“4”]但它一直让我回归[“0” ”, “2”, “3”, “4”]

1 个答案:

答案 0 :(得分:-1)

试试这个

var widgets=["1","2","3","4"];
var storeWidgets;
storeWidgets = widgets.slice(0);;
widgets[0]=0;
alert(widgets);
alert(storeWidgets);