SCENARIO
I have got this class:
var AssocArray = function(){
    var collection = new Object();
    this.add = function(id, o){
        collection[id] = o;
    }
    this.remove = function(id){
        delete collection[id];
    }
    this.getById = function(id){
        return collection[id];
    }
    this.get = function(){
        var res = collection;
        return res;
    }
}
var myAssoc = new AssocArray();
myAssoc.add("11",{ "packageId": "11", "machineId": "1", "operationType": "Download"});
myAssoc.add("12",{ "packageId": "12", "machineId": "1", "operationType": "Download"});
myAssoc.add("14",{ "packageId": "14", "machineId": "1", "operationType": "Download" });
if(myAssoc.getById("20")) myAssoc.remove("20");
if(myAssoc.getById("11")) myAssoc.remove("11");
console.log(myAssoc.get());  //returns Object {12: Object, 14: Object}
Question
Everything works right. But if I do this:
(myAssoc.get())[10] = {};
console.log(myAssoc.get());  //returns Object {10: Object, 12: Object, 14: Object}!!
the private member collection is eventually modified. That is unexpected (and unwanted!).
- What is wrong?
- How to make get()to return a copy of thecollectionmember, and not the member itself?
EDIT
I have read this question. So cloning the collection member could do the job. Is there in design patterns another way to mamage private members and the related read-only properties?
 
     
     
     
    