JSFiddle - React, Tailwind, and code Playground

JavaScript

function test(){
    
    // I want this to be safe from outside influence
    var a = [{
        val: 1   
    },{
        val: 2   
    }];
    
    return {
        all: function(){
            return a.slice();
        }
    }
}

var instance = test();
var copy = instance.all();

copy[0].val = 'wrong';

console.log('the object refs are NOT new, so the copy can still overwrite the original:');
console.log(instance.all()[0].val,copy[0].val);

console.log('the array ref is new, so wiping the copy doesn\'t affect the original:');
copy = [];
console.log(copy);
console.log(instance);