i have a huge problem figuring out scopes and clauses. I want to have a tmp variable, that i remove one element from. But the global variable should stay intact. This is what iv´e come up with. This;
var test = ['test', 'huhu'];
function foo(bar) {
var tmpTest = test;
var index = tmpTest.indexOf(bar);
if (index > -1) {
tmpTest.splice(index, 1);
}
console.log(tmpTest);
}
foo('huhu');
console.log(test);
Should produce this;
test
test, huhu
But produces this;
test
test
I also tried to make var tmpTest = new Array(test);
But this stops the splicing from working. I guess what´s happening is that when i set tmpTest = test
, tmpTest
just becomes a reference to the original test
variable? How can i come around this?