Using Javascript, I have two arrays called “array1” and “array2”.
I set currentArrray to be array1. Later when user clicks on some button, currentArray switches to Array2 and back to array1 if I click on it again.
This way, whenever there is any part of the code that needs to use an array, I just use something like in pseudocode:
{currentArray}.getText;
{currentArray}.getText;
Is it possible to do something like this? If so, how?
I know you can use if statement, but because currentArray is used in different parts of code, it would be nice if I had a simple way like this where all I have to modify is currentArray variable so I dont have if statements scattered all over the place checking what currentArray is.
You can create a variable called “currentArray” and just set it to refer to whatever other array you want.
If you’ve got some function that refers to a particular global variable:
Then it’s going to refer to “array1” no matter what. You can change the value of “array1” of course, but you can’t make that function refer to “array2” any other way. Functions in JavaScript are immutable.