melanke / Watch.JS

watch the changes of any object or attribute
Other
2.14k stars 219 forks source link

Issue with object within array #93

Closed baxter971 closed 9 years ago

baxter971 commented 9 years ago

Hi,

The issue is encountered on an object we put in an array. Here is my test script:

var DOManager = (function(){
            var dolist = {};
            var data = [{name: "Hi",surname: "hello"}];

            var _addData = function(_data){
                           data.push(_data);
            };
            dolist.addData = _addData;
            var _getData = function(){
                           return data;
            };
            dolist.getData = _getData;
            return  dolist;
});

var DO = new DOManager();
console.log(DO.getData());

watch(DO.getData(), function(prop, action, newvalue, oldvalue){
    console.log("Action : " + action);
});
var _data = "world";
DO.addData(_data);

console.log(DO.getData());

The issue is that in the first console.log we should get an object with “hi” and “hello”, and the second console.log an object with “Hi”,”Hello” and “World”. However the watch function seems to modify also the first log and we can’t understand why. This issue happens only when we put an object inside an array. Have you any idea why this is?