EDIT: I realize many of the things I am running into are artifacts of the updated V8 GAS runtime. The question is updated now to reflect these changes.
I am encountering some interesting behavior in Google Apps Script. I have written a small demonstration function which demonstrates this behavior.
The function below is a wrapper function which allows me to test the "phaseToSequenceIssueDemonstration" function.
function testPhaseToSequence(){
var phaseSequenceArray = phaseToSequenceIssueDemonstration([[26.1877,21.333],[17.828,-102.813],[10.812,139.288]]);
Logger.log("PHASE SEQUENCE ARRAY:" + phaseSequenceArray);
}
This is the "phaseToSequenceIssueDemonstration" function which the wrapper function above is calling:
function phaseToSequenceIssueDemonstration(phaseArray){
Const polarArray = phaseArray.splice();
Logger.log("Polar array before function call: " + polarArray);
Logger.log("Phase array before function call: " + phaseArray);
var cartArray = arrayPolarToCart(polarArray);
Logger.log("Polar array after function call: " + polarArray);
Logger.log("Phase array after function call: " + phaseArray);
}
As you can see, I'm assigning polarArray to the variable phaseArray, which is passed in the wrapper function. I then push to the logger the values of both variables, which should be the same.
I am then creating another variable "cartArray," passing polar Array into this function, and storing the result.
The arrayPolarToCart function is seen below. It takes an array, performs a manipulation, and returns it in it's new form:
function arrayPolarToCart(array){
//Contains the continuous subtraction of all vectors in the array.
const total = array.slice();
let cartesianArray = total.slice();
for(var i = 0; i<array.length; i++){
var currentVector = total[i];
var currentMag = total[i][0];
//Logger.log(currentMag);
var currentAngle = total[i][1];
// Logger.log(currentAngle);
var currentX = currentMag*Math.cos(Math.PI*(currentAngle/180));
var currentY = currentMag*Math.sin(Math.PI*(currentAngle/180));
cartesianArray[i][0] = currentX;
cartesianArray[i][1] = currentY;
}
//Logger.log(total);
return cartesianArray;
}
Notice that there is no further change or assignment of either the phaseArray or polarArray variables after the first line of the function.
Here's what the loggers seen in the function print:
Notice that both the variable passed into this function, and the variable assigned at the beginning have changed after the assignment of the cartArray variable, despite there never being an assignment statement in the function to change them and assigning const and splice where appropriate to create new references.
How could this be?