Within this code:
window.g_b_editEnable = false;
window.g_a_PreEditData = 'hello';
function EditRow(EditButton){
if(!window.g_b_editEnable){
window.g_b_editEnable = true;
var a_i_Pos = a_o_table.fnGetPosition( EditButton.parentNode );
aData = a_o_table.fnGetData( a_i_Pos[0] );
console.log(window.g_a_PreEditData);
window.g_a_PreEditData = aData;
console.log(window.g_a_PreEditData);
aData[0] = '<button type=submit name=save>Save</button><button onclick=ResetTableFields(this) name=reset>Reset</button>';
for(count = 1; count < aData.length; count++){
aData[count] = '<input type=text value='+aData[count]+'></input>';
}
a_o_table.fnUpdate( aData, a_i_Pos[0], undefined, false, false);
console.log(window.g_a_PreEditData);
}else{
return 1;
}
}
I was attempting to utilize a global variable (window.g_a_PreEditData) to store a value and pass it to another function.
However, within the mentioned function, I save the value of a local variable in the global one, then update the local variable. Strangely, the global variable also gets updated simultaneously with the local one.
The displayed console logs are as follow: Hello (initialization value) Value1 (local variable's value) Value2 (local variable's updated value)
I intended for it to display: Hello Value1 Value1
If I create a new variable like an array called 'aNewData' to store the updated values instead of modifying aData directly, the global variable window.g_a_PreEditData remains unaffected. But why does this unexpected behavior occur? My understanding was that JavaScript passed variables by value rather than reference. Therefore, once the global variable is assigned the local values, any further changes made to the local variable should not affect the global one unless reassigned... or so I thought!
Could someone enlighten me on why this anomaly is happening? Thank you!