我正在创建一个基本数组,我将其附加到。这使我的基础数组:
make_shell = function (opts) {
var x, shell, shell_row;
shell = [];
for (x = 0; x < opts.points; x += 1) {
shell_row = [];
shell_row.push(opts.delta * x);
shell.push(shell_row);
}
return shell;
};
raw_graph = make_shell({
"points": data_points_per_channel,
"delta": x_delta
});
然后我想运行几个方法,每个方法都需要raw_graph
的干净副本。但是,如果我这样做:
make_graph_struct = function (opts) {
var j, k, len, struct, row, runner, series;
console.log("RAW")
console.log(opts.shell);
return struct;
};
// Graph #1 - works
var test_a = new Dygraph(
document.getElementById("graph-a"),
make_graph_struct({
"shell": raw_graph.slice(0),
"display": [[0,0]],
"total_channels": total_channels,
"points": data_points_per_channel,
"data": tmp_data
}),
{...}
);
// Graph #2 - ERROR raw_graph now includes values added in first call
var test_b = new Dygraph(
document.getElementById("graph-b"),
make_graph_struct({
"shell": raw_graph.slice(0),
"display": [[1,4000]],
"total_channels": total_channels,
"points": data_points_per_channel,
"data": tmp_data
}), {...}
);
问题是,raw_graph
仅在第一次通话时以原始形式出现。之后,每次通话都会在相应的通话中添加我需要的值,因此 Graph2 ,raw_graph
将包括
[setter, value_from_call_1]
问题:
如何正确使用slice
或为什么当切片只能进行克隆时,我的每个图表值会在raw_graph
中结束?
编辑:这是一个jsbin。检查console.log jsbin