Charlie_S24
asked on
Why is my local Javascript array's being modified in external functions?
Although I thought I understood how local and public variables work with JavaScript, I still have a function modifying my two-dimensional array. I'm not sure how to prevent that from happening. (Have I declared the array properly? Am I passing it properly?)
My goal is to have a two-dimensional array in the main routine whose data is passed to a function that uses the data, but leaves the original array in the main routine untouched.
Below is a little bit of fictitious code that duplicates the problem.
Your help would be appreciated.
<html>
<body>
<p id="demo">Click the button to test code.</p>
<button onclick="myFunction()">Try it</button>
<script type="text/javascript">
function myFunction()
{
var aryFirstArray = new Array(1)
aryFirstArray[0] = new Array()
aryFirstArray[0][0] = "I am expecting the 2nd dimension to still be 5: "
aryFirstArray[0][1] = 5
myOtherFunction(aryFirstAr ray)
var x=document.getElementById( "demo");
x.innerHTML=aryFirstArray;
}
function myOtherFunction(aryDiffere ntArray)
{
aryDifferentArray[0][1] = 4
}
</script>
</body>
</html>
My goal is to have a two-dimensional array in the main routine whose data is passed to a function that uses the data, but leaves the original array in the main routine untouched.
Below is a little bit of fictitious code that duplicates the problem.
Your help would be appreciated.
<html>
<body>
<p id="demo">Click the button to test code.</p>
<button onclick="myFunction()">Try
<script type="text/javascript">
function myFunction()
{
var aryFirstArray = new Array(1)
aryFirstArray[0] = new Array()
aryFirstArray[0][0] = "I am expecting the 2nd dimension to still be 5: "
aryFirstArray[0][1] = 5
myOtherFunction(aryFirstAr
var x=document.getElementById(
x.innerHTML=aryFirstArray;
}
function myOtherFunction(aryDiffere
{
aryDifferentArray[0][1] = 4
}
</script>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.