In the code below, I expected that the result of the 3 logs would be the same.
But they are not.
var m = (function () {
var arr = [
{a: 1, b: 2},
{c: 3, d: 4}
];
return {
getArr: function () {
return arr;
}
};
})();
var myArr = m.getArr();
console.log(myArr);
myArr.pop();
console.log(m.getArr());
What is the best way to ensure that the array is not passed as a reference?
Use return arr.map()
?